Python OOP Tutorial (Object Orientated Programming ) - Static Methods and Class Methods

Tech With Tim · Intermediate ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

This video tutorial by Tech With Tim covers the concepts of static methods and class methods in Python, including their implementation and use cases, as well as class variables and decorators.

Full Transcript

hey guys and welcome back to the fifth video in my Python objects and classes tutorial series in today's video I am going to be talking about class methods static methods and class variables now these are very important and very useful especially if you're coding large projects with multiple different classes and they are fairly complex but I'm gonna try my best to explain them right now so pretty much you can see here I've already set up a class and I've already actually coded all the methods and class variables and all the stuff we're going to deal with and I'm just gonna go through and talk about it because it just saves a bit of time it makes things a bit easier so first of all let's talk about class variables now to create a regular variable within a class you can see that we have something like self-thought name equals name and we've been over this we know how this works now when you create a class variable typically you do this at the top of the class and you just simply write like a variable just not inside of one of your methods so you can see up here I have a list says dogs equals and then a list if I wanted to have something like X I could say X equals 5 or X equals 5 and I could put that at the top of my what do you call it class and then to reference that you have to be inside of the class so this sometimes is useful if you want to have variables that every object in your class is going to need to use and you don't want to have to do like self-thought x equals x within your initialization and it's just better practice to put variables that aren't going to be like statically used inside of the class if that kind of makes sense so the way that you reference these variables is the same as you would reference an attribute within your regular class so you can see here like I do self the name equals name to create a new variable self that name when I want to reference it later I would have to type self dot name right it's the same way to reference these class variables so you can't whereas you could but you wouldn't want to have the same name as this like in here and if you did that it would just overwrite this I see you'll see what I mean in a second but anyways what I'm essentially doing here inside of my initialization is I'm appending every single dog object that we create into the list dogs now this list dogs be against the class dog not to the actual instance of dog so Tim and Jim if I reference dogs and I print that out will be the same value it doesn't change dogs or like dogs that's not specific to Tim and it's not specific to Jim it's specific to the entire class for every dog object so it's the same for all that and Exce see it like five is gonna be the same for obviously Tim and Jim and all other dog objects I hope that kind of makes sense we'll walk through it in just a second okay so let's just first we'll just print and figure out like what's the point of this class variable like what can we do with this well first of all the good thing about class variables is if you want to access them you don't have to have an instance of the dog object to do so so to see this typically like if I want it to call so if I remove this class method I remove the static method you want a Cole method on a dog object you'd have to do name the dogs like Tim and then dot mark or Tim dot I don't know whatever else that we have like dot add dot add weight whatever you want to do okay but in this case we can just actually call it on the name of the class so if I cube dog dot and then you can see it's already coming up here dogs like this we should be able to print this to the screen and you can see that we do end up being able to print this to screen with a dog object and another dog object so the main difference here with class variables is that you can call them with you can still call like I could still do tim dot dogs but it I can also call them with the actual name of the class and that's what makes it useful so you don't have to have an instance to call that what do you call it variable if that makes sense and I guess that's all I'm going to explain cut after class variables because they're not that complicated if you play around with them you should be able to figure that out figure them out okay so next we're gonna talk about static and class methods now you can see here I have add static method and that class method these are known as decorators and you put these above your method like directly above them like this if you want to indicate that they are going to be a special type of method and that's what these two methods are so I'll start I'll first start talking about class methods because we kind of already have touched on it with class variables so the way that a class method works is that you can actually call it on simply the name of the class so you can see inside of here my parameters I only have one thing and it's called CLS it's not called self it's called CLS what this means is the name of the class so if I remove Tim and Jim so I say and just comment that out like that whatever then what I can do is I can say dog dot and then what do you call it numb dogs like this and this will actually work and you can see that I'm not calling it on an instance or like of the dog class or like a object of dog class I'm just simply calling it on the class so if I do this you can see that it gives me zero and that that works right now again you can call it on what do you call it like you can call it on the instance like if you say 10 equals dog and named Tim I can still call it on Tim and this is still gonna work fine it's just that you don't need to do that right you can do it by just calling with the name now static methods are a little bit different they actually don't need the class to to be called so it doesn't pass in the class so that means that you can't reference anything within the class so the whole point of having this class passed in when you call this method is so that you can use class variables and you can use other methods within it with inside the class so for example if within num dogs I wanted to use a static method or I wanted to call it initialization or I wanted to do something that revolved around the dog class I would need the class name right so that's why it's passed through but with static methods we only pass whatever parameters we want so we don't have to have a self we don't have to have a class and we don't even have to have any parameters but in this case I'm giving one parameter n so I'm going to show you how this works so I've completely removed any instances of dog in my program there's no objects we're not printing anything whatever okay what I'm gonna do now is I'm gonna call bark without passing through any object or any class name and you can see how this works so what I have to do to do this is it has to know see where bark is right so it's inside the dog claw so I have to start by typing dog but then I can type bark like this and give it a number and you can see here I just wrote a little comment barks end time so if I run this you can see we get bark bark bark bark now notice that with inside the spark class I don't do anything like I don't touch any attributes I don't touch any class methods and there's actually no way for me to do that so if I were to try to say I wanted to get the value of the length of dogs right with inside the static method well I would be unable to do that because I don't have self write self is it's not working I don't have a class so how am I gonna be able to call a classroom variable or another class method I actually can't do that so the point of static methods is one you're just gonna be using them as a function but you want to organize them within a class so a really good use of static methods for example say you were created like a class and you called it math okay in here instead of having like an initialization and all this stuff you just had a bunch of static methods now why why do you put them in this math class why not just create them as functions well the thing is you want to build import modules within Python you guys have probably seen this before right you can import other files that you've created into like main files so when you create a bunch of like math methods or whatever way you do is you'd say class math and then you just say like app static method and then you define and you'd say like ah like this okay and you say like X and X 2 and then you could return X + X 2 and that way you don't have to create an instance of math you don't have to say like M equals math right you can just say math dot add give it two values and it will return it to you and you can do math odd subtract and you can organize all like the functions that you wanted to use within that class and then class methods right are more use for like if you want to access class variables without having to pass in an object because it's just gonna automatically pass whatever the class name is that you're giving it I hope that that makes sense now these I'll touch on these really quickly in case I didn't really talk about them these are known as decorators I know it's you're named but pretty much this just denotes the fact that you are creating a class method and you are creating a static method because otherwise it's probably gonna get them out of you and tell you that you need to type itself or you need to type in like another parameter or something like that so by doing this not only is it a visual representation for anyone that's reading your code but you're also just I'm telling Python that this is gonna be a static method this gave me a class method obviously you can have as many static methods as you want as many class methods as you want and actually really useful so anyways I'm gonna wrap up the video here if you guys have any questions if I don't need this stuff make sure you leave a comment down below and in the next video I'm going to be talking about public and private classes in Python anyways if you guys enjoyed the video please make sure you leave a like and subscribe and I will see you again in the next one [Music]

Original Description

Static and class methods in python. This video explains the difference between static and class methods and how to properly implement them in python. I also mention class variables as well. WEBSITE: https://techwithtim.net/tutorials/python-programming/classes-objects-in-python/static-and-class-methods/ ************************************************************** Support the Channel: https://www.patreon.com/techwithtim Join my discord server: https://discord.gg/pr2k55t ************************************************************** Please leave a LIKE and SUBSCRIBE for more content! Tags: - Tech With Tim - Pygame - Python Tutorials - Object and Classes Tutorial - Objects and Classes Python - OOP Python - Object orientated python - Static and class methods
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video tutorial covers the concepts of static methods and class methods in Python, including their implementation and use cases, as well as class variables and decorators. It provides a comprehensive understanding of how to use these concepts in Python programming.

Key Takeaways
  1. Create a class variable at the top of a class
  2. Create a class with class variables and methods
  3. Call class methods with the class name
  4. Call static methods without the class name
  5. Use decorators to denote static and class methods in Python
💡 Static methods in Python can be used as functions within a class without having access to class attributes or methods, while class methods are used to access class variables without having to pass an object.

Related Reads

📰
My AI Bill Dropped 95% When I Switched to Chinese Models
Switching to Chinese AI models can significantly reduce costs, with one company experiencing a 95% drop in their AI bill, making it a viable option for businesses looking to cut expenses.
Dev.to AI
📰
AI Dubbing for Short Drama: The 2026 Playbook That Actually Scales
Learn how to scale AI dubbing for short drama with the 2026 playbook, improving efficiency and quality in audio post-production
Dev.to AI
📰
Why AI App Backends Are Becoming Accounting Systems
Learn how AI app backends are evolving into accounting systems and why it matters for SaaS businesses
Dev.to · StructureIntelligence
📰
I Let AI Build My WordPress Forms for a Week. Here’s What Actually Worked.
Learn how AI can automate building WordPress forms and what actually works in practice
Medium · AI
Up next
Your Competitors Are Using This: The AI SEO Toolkit for Brand Mentions
Matt Tutorials
Watch →