Intermediate Python Tutorial #2 - Static and Class Methods
Key Takeaways
This video tutorial covers the use of static and class methods in Python, explaining their differences and use cases, and provides hands-on examples of creating and using these methods within a class.
Full Transcript
hey guys and welcome back to another YouTube video in today's video the second video in my intermediate Python tutorials I'm gonna be going over static and class methods so pretty much these are just different things you can use within a class and I'm gonna explain what they do and why they're useful so without further ado let's go ahead and get started so because these two things methods obviously a part of a class use the class we need to first create a class so in this instance I'm just gonna make a class I'm gonna call it person it is a class object and then I just start off by making my initialization method here give itself I'm also gonna give it name and age and we move to the next time so let's just assign our variable skillet here self done name is name and self dot H is equal to H okay the next method I'm gonna make this one is going to be called get population this type self actually here I'm gonna type CLS and I'm gonna get into what this does in a little bit and I'm gonna return CLS dot population and now I just remembered that I actually forgot to make this up here I'm gonna make a class variable where I'm going to say population is equal to 50 like so gain one more method this one I'm gonna call define and then is adult and then in here I'm just gonna put age I'm gonna say return age greater than you go up to 18 okay I'm also gonna make one more method down here just to show you the difference between a bunch of these and I'm just gonna call define display okay and so it's gonna take self and that's actually all we need we're just gonna print to the screen in this one we're gonna say self dot name and then comma is and then comma self dot age , years old so this should if I did this right it's gonna say wherever the person's name is is and then their age years old so for me 18 years old so now let's create a new instance of this class down here so I'm just gonna call this one a new person I set it equal to a person whose name is Tim and whose age is 18 like so and now I'm just gonna go through this class and kind of go through what we've actually done here so pretty much what I've started off by doing is I've created a class variable population goes 50 I've done my constructor method which is just gonna initialize my variables name and age and I've created two methods in here which actually aren't done in the called gap population and is adult now the name of this video is class methods and static methods so one of this is a class is a class method and the other one is a static method go ahead and guess which one you think is which so I'm gonna tell you right now the first one is actually a class method and the next this one here is a static method and the way that we denote these in Python is by putting something called a decorator above these methods so just an @ sign and then the name so in that case static method a class method in this case static method now what is a cloth class method and what is a static method and the best way to explain these is to use an example so pretty much a class method means that you can call it on any instance of a class so you don't need to have for example I don't know what to say you don't have to have an object already created of that class you can just call it on the class so for here for example I've done something like a new person in it typically if I didn't have any other any static methods your class method I would just say like new person display or new person get population with this class method allows us to do is do something like this so we can do person doc get population now person is simply the name of our class it's not the name of an object of that class so a new person is an object of class person whereas it's just the name of the class and if I do that and I print this to the screen so I print person dog cat population assuming I have no errors here oh it says it's because I haven't put little brackets here my back dog cat population it pops up to the screen and it gives us the value of 50 and again we didn't have to create an object to use that method because it is a class method I hope that makes sense I'll try to explain it maybe one more time really quickly pretty much you don't need to create an object of the class to use any methods that are decorated as class method now also in class method old it's passed to the class method is well the class because it's not actually an object we don't need this self parameter here like we need in our initialization method and in any other regular methods that we have we just need one variable we can call whatever we want in this case I call it CLS which is gonna store well what class you're getting pretty well and then we can add any other parameter so if you want so I get out X okay y infinitely many after that we just need to make sure that we have at least one in there because for example if I remove this CLS like that it's gonna say take zero positional argument but one was given because the class name is automatically sent into this method when we call it okay the next type of method is static method and this one is similar to class method except it can be called without using that class I want to say so it doesn't take a self parameter it doesn't take a class parameter so you don't actually need anything in here in this case I have age just because I want to be able to compare age but it doesn't need any parameters if I did something like this it would work fine so the way we denote that again is with the @ sign static method just above where the method is defined and to use this method I can do something like person dot is adult and then in here if I put something like five we're gonna get back the value of false which you can see here if I do a value like 21 we get a value of true now why is this useful what's different than class method static man it is just used when you don't need self and you don't again need the actual object and it's just a good organizational way of storing a bunch of method so for example if you had like I don't know maybe a math class that you created and you wanted to have a few math objects and then you wanted a bunch of static methods that we're all stored under that math so for example when you use the math module in Python and use math dot round or math dot so on that's an example of possibly a static method or just a function within the math class you want to organize all your static methods which int within that class so you would call like whatever the class name is math and then dot the method and then you give it an argument and it will return that and work fine I hope that kind of makes sense it is hard to explain if you don't have like a lot of good use cases for it class method it just takes the actual class and then it can access anything within the class that's public to the class so here you see I do CLS population and population is a variable defined up here this static method it can't access this population variable because it doesn't have access to the class name right it doesn't it's not passed that information so it can only use the parameters that you pass it it can't use any better defined within the class and that's really the best way I can kind of explain it to you guys and a good way to understand how to use this and why they're useful is to use them in your own use cases so again static method you can call just by calling the person the name of the class and then that method given whatever parameters it doesn't need any parameters if I do something like this and I just put like five greater than or equal to eighteen this still works you don't need any parameters whereas a class method you need one parameter at least a minimum which is going to be that class name and it has access to anything within the class so any variables you define for other static methods within like within this class method you could call another static method and so on so yeah I hope that makes sense to you guys it is kind of hard to explain the static in class methods but they are really useful especially if you're trying to organize things and when you get further on with object-orientated programming and python you definitely notice that you will use these a lot anyways that's been it for this video please make sure you guys leave a like on the video and subscribe and I will see you again in the next one [Music]
Original Description
Welcome to a new series! Intermediate Python Tutorials!
Today's Topic: static and class methods, these are different types of method that you can use within a class. They are especially useful when doing OOP programming and have many use cases.
In this set of videos I will be explaining more advanced programming concepts and showing you intermediate-advanced tools that you can use in python. A lot of these tools will help you to solve problems/code more efficiently and will save you a ton of time!
Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/intermediate-python-tutorials/static-class-methods/
Twitter: https://twitter.com/TechWithTimm
Future topics:
3. Map Function
4. Filter Function
5. Lambda Functions
6. Intro to Collections
7. Collections: named tuple
8. Collections: deque
9. Collections: orderedDict
10. Collections: defualtDict
11. docstrings
Want To Support This Channel?
Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Pygame
- Python Tutorials
- Intermidiate Python Tutorials
- static and class methods
- static methods vs class methods
- object orientated programming
- objects in python
- classes in python
- python intermediate
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI