Intermediate Python Tutorial #2 - Static and Class Methods

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

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 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 teaches the basics of static and class methods in Python, including how to define and use them, and provides examples of their application in object-oriented programming. It covers the differences between static and class methods and how to use them effectively. By watching this video, viewers can learn how to create and use static and class methods in their own Python programs.

Key Takeaways
  1. Create a class called Person
  2. Define a class method get_population
  3. Define a static method is_adult
  4. Create an instance of the Person class
  5. Call the methods of the Person class
  6. Create a class with a static method
  7. Call the static method without creating an object
  8. Use the class name to call the static method
  9. Pass arguments to the static method
💡 Static methods can be called without creating an object and do not have access to class variables, while class methods require the class name as an argument and can access class variables.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →