- Python OOP Tutorial 3: classmethods and staticmethods

Python OOP Tutorial 3: classmethods and staticmethods

In this Python Object-Oriented Tutorial, we will be learning about classmethods and staticmethods. Class methods are methods that automatically take the class as the first argument. Class methods can also be used as alternative constructors. Static methods do not take the instance or the class as...
In this Python Object-Oriented Tutorial, we will be learning about classmethods and staticmethods. Class methods are methods that automatically take the class as the first argument. Class methods can also be used as alternative constructors. Static methods do not take the instance or the class as the first argument. They behave just like normal functions, yet they should have some logical connection to our class. We will look at some examples of both of these in order to understand both in depth. Let's get started.

Python OOP 1 - Classes and Instances - https://youtu.be/ZDa-Z5JzLYM
Python OOP 2 - Class Variables - https://youtu.be/BJ-VvGyQxho
Python OOP 3 - Classmethods and Staticmethods - https://youtu.be/rq8cL2XMM5M
Python OOP 4 - Inheritance - https://youtu.be/RSl87lqOXDE
Python OOP 5 - Special (Magic/Dunder) Methods - https://youtu.be/3ohzBxoFHAY
Python OOP 6 - Property Decorators - https://youtu.be/jCzT9XFZ5bw

The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Object-Oriented


✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1

✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer

▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter -
Instagram - https://www.instagram.com/coreymschafer/

#Python

#Python #Python Tutorial #Python Classes #classmethod #staticmethod #classmethods #staticmethods #Classes #OOP #Object Oriented #Python Objects #Class Variables #Class Attributes #Class Methods #Static Methods #Python OOP #Class #Python Class #Decorators #Python Decorator #classmethod vs staticmethod #Object Oriented Programming #@classmethod #@staticmethod
I paused at   to checkout video on Decorators which I paused again to checkout video on Closures which I paused, yet again to checkout video on First Class Functions. Glad, I did. Thanks for the awesome videos. - Python OOP Tutorial 3: classmethods and staticmethods

I paused at to checkout video on Decorators which I paused again to checkout video on Closures which I paused, yet again to checkout video on First Class Functions. Glad, I did. Thanks for the awesome videos.

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:01:17 - 00:15:20
At  I am following along and it is not changing the raise amount by accessing the class method. Does anyone have any idea why this is happening? - Python OOP Tutorial 3: classmethods and staticmethods

At I am following along and it is not changing the raise amount by accessing the class method. Does anyone have any idea why this is happening?

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:04:02 - 00:15:20
i just rewatched a segment of the previous video and  in this video and i think i'm right. - Python OOP Tutorial 3: classmethods and staticmethods

i just rewatched a segment of the previous video and in this video and i think i'm right.

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:04:05 - 00:15:20
just so im clear: at  using a classmethod on emp_1 to change the raise to 1.05 changes it for other instances too? - Python OOP Tutorial 3: classmethods and staticmethods

just so im clear: at using a classmethod on emp_1 to change the raise to 1.05 changes it for other instances too?

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:04:05 - 00:15:20
im at a hold up  in. got error AttributeError: 'Employee' object has no attribute 'set_raise_amt' my code looks identical to yours. im using thorny on my pi as well if that makes any difference. Help would be appreciated. Thank you - Python OOP Tutorial 3: classmethods and staticmethods

im at a hold up in. got error AttributeError: 'Employee' object has no attribute 'set_raise_amt' my code looks identical to yours. im using thorny on my pi as well if that makes any difference. Help would be appreciated. Thank you

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:04:11 - 00:15:20
Class method as alternative constructors - Python OOP Tutorial 3: classmethods and staticmethods

Class method as alternative constructors

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:04:25 - 00:09:10
I noticed a small mistake at .When you split the string, you create new strings including the pay variable.If you were to execute the apply_raise() function on that new_emp_1 variable, the function would try to multiply a float by a string, getting an error.We need to convert that string to an integerSo that line 34. should read:    new_emp_1 = Employee(first, last, int(pay))Same goes for the subsequent from_string classmethod created using the same way. - Python OOP Tutorial 3: classmethods and staticmethods

I noticed a small mistake at .When you split the string, you create new strings including the pay variable.If you were to execute the apply_raise() function on that new_emp_1 variable, the function would try to multiply a float by a string, getting an error.We need to convert that string to an integerSo that line 34. should read: new_emp_1 = Employee(first, last, int(pay))Same goes for the subsequent from_string classmethod created using the same way.

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:05:46 - 00:15:20
At  is it possible to do call self._init_ inside the alternative constructor after parsing the original emp_str? - Python OOP Tutorial 3: classmethods and staticmethods

At is it possible to do call self._init_ inside the alternative constructor after parsing the original emp_str?

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:08:00 - 00:15:20
At  would you be able to write:cls.first, cls.last, cls.pay = ...instead of:cls(first,last,pay) - Python OOP Tutorial 3: classmethods and staticmethods

At would you be able to write:cls.first, cls.last, cls.pay = ...instead of:cls(first,last,pay)

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:08:00 - 00:15:20
couldnt you just do all of this in the constructor method? i mean i guess this can be useful if sometimes new employee is ordinarily instantiated and sometimes the data has to be parsed from a string - Python OOP Tutorial 3: classmethods and staticmethods

couldnt you just do all of this in the constructor method? i mean i guess this can be useful if sometimes new employee is ordinarily instantiated and sometimes the data has to be parsed from a string

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:08:39 - 00:15:20
is a good pause stop - Python OOP Tutorial 3: classmethods and staticmethods

is a good pause stop

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:08:41 - 00:15:20
Best Python resource on YouTube.  One question please, at   I tried add  one line for a verification purpose: new_emp_1.apply_raise()     and found  an issue:  self.pay = int(self.pay * self.raise_amt)
   TypeError: can't multiply sequence by non-int of type 'float'.  Not sure what I did wrong?  Same issue  occurred if I run your codes.  Please help. Thank you very much. - Python OOP Tutorial 3: classmethods and staticmethods

Best Python resource on YouTube. One question please, at I tried add one line for a verification purpose: new_emp_1.apply_raise() and found an issue: self.pay = int(self.pay * self.raise_amt) TypeError: can't multiply sequence by non-int of type 'float'. Not sure what I did wrong? Same issue occurred if I run your codes. Please help. Thank you very much.

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:09:10 - 00:15:20
Real World example with datetime module - Python OOP Tutorial 3: classmethods and staticmethods

Real World example with datetime module

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:09:10 - 00:10:20
example of datetime module - Python OOP Tutorial 3: classmethods and staticmethods

example of datetime module

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:09:13 - 00:15:20
This really a good explanation. Being consistent with your methodology against known examples such as the datetimemodule  () reinforces the learning experience. Thank you Corey. - Python OOP Tutorial 3: classmethods and staticmethods

This really a good explanation. Being consistent with your methodology against known examples such as the datetimemodule () reinforces the learning experience. Thank you Corey.

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:09:20 - 00:15:20
Hey there. I explain this in my conditionals video. I talk about that at the  mark in that video. I timestamped a link here:https://youtu.be/DZwmZ8Usvnk?t=9m27s - Python OOP Tutorial 3: classmethods and staticmethods

Hey there. I explain this in my conditionals video. I talk about that at the mark in that video. I timestamped a link here:https://youtu.be/DZwmZ8Usvnk?t=9m27s

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:09:27 - 00:15:20
why there is underscore before time.localtime(t)? - Python OOP Tutorial 3: classmethods and staticmethods

why there is underscore before time.localtime(t)?

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:09:27 - 00:15:20
Thank you for adding a real world example - Python OOP Tutorial 3: classmethods and staticmethods

Thank you for adding a real world example

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:09:29 - 00:15:20
Static Method - Python OOP Tutorial 3: classmethods and staticmethods

Static Method

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:10:20 - 00:15:20
Static_Methods = - Python OOP Tutorial 3: classmethods and staticmethods

Static_Methods =

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:10:21 - 00:15:20
Best way to describe static methods is: Methods which function irrespective of the class or instance state. They are stateless... - Python OOP Tutorial 3: classmethods and staticmethods

Best way to describe static methods is: Methods which function irrespective of the class or instance state. They are stateless...

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:11:24 - 00:15:20
code golf:  you could replace lines 32-34 with one:return not day.weekday() in [5, 6] - Python OOP Tutorial 3: classmethods and staticmethods

code golf: you could replace lines 32-34 with one:return not day.weekday() in [5, 6]

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:12:24 - 00:15:20
I found for myself this syntax when you need a quick True/False check: - Python OOP Tutorial 3: classmethods and staticmethods

I found for myself this syntax when you need a quick True/False check:

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:12:44 - 00:15:20
So, in , you used day.weekday(), but the weekday function has not been initialised before or has been mentioned before. How would the program know it has to take the modulus of the value with 7 and use that result to check whether it is a weekday? - Python OOP Tutorial 3: classmethods and staticmethods

So, in , you used day.weekday(), but the weekday function has not been initialised before or has been mentioned before. How would the program know it has to take the modulus of the value with 7 and use that result to check whether it is a weekday?

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:13:08 - 00:15:20
on line 32 :Why not use in [5, 6] instead? - Python OOP Tutorial 3: classmethods and staticmethods

on line 32 :Why not use in [5, 6] instead?

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:13:14 - 00:15:20
Quick question, can we access the is_workday method using the instance name atemp1.is_workday(my_date)Instead of, Employee.is_workday(my_date)  ? - Python OOP Tutorial 3: classmethods and staticmethods

Quick question, can we access the is_workday method using the instance name atemp1.is_workday(my_date)Instead of, Employee.is_workday(my_date) ?

Python OOP Tutorial 3: classmethods and staticmethods
2016年07月12日 
00:13:44 - 00:15:20

Corey Schafer

※本サイトに掲載されているチャンネル情報や動画情報はYouTube公式のAPIを使って取得・表示しています。

Timetable

動画タイムテーブル

動画数:236件

how to override existing environment variables. (This is new to me) - Python Tutorial: Securely Manage Passwords and API Keys with DotEnv

how to override existing environment variables. (This is new to me)

Python Tutorial: Securely Manage Passwords and API Keys with DotEnv
2024年04月18日  @JorgeEscobarMX 様 
00:10:42 - 00:16:58
the future spoken is my current present. Unless I'm completely desperate, I'm not hitting reddit or google/stack overflow for most roadblocks. - How to Use ChatGPT as a Powerful Tool for Programming

the future spoken is my current present. Unless I'm completely desperate, I'm not hitting reddit or google/stack overflow for most roadblocks.

How to Use ChatGPT as a Powerful Tool for Programming
2023年05月22日  o k t o b e r 様 
00:12:22 - 00:31:08
Good video as always😉  I remember asking that too lol - How to Use ChatGPT as a Powerful Tool for Programming

Good video as always😉 I remember asking that too lol

How to Use ChatGPT as a Powerful Tool for Programming
2023年05月22日  kami 様 
00:28:19 - 00:31:08
@ - pure gold, pure gold! <3 - How to Use ChatGPT as a Powerful Tool for Programming

@ - pure gold, pure gold! <3

How to Use ChatGPT as a Powerful Tool for Programming
2023年05月22日  ulf gjerdingen 様 
00:28:26 - 00:31:08
You almost had me there haha! ChatGPT can be wrong but it certainly wasn't wrong about those top notch resources! I massively appreciate all the uploads and I hope they keep coming supercharged with Chat GPT. Thanks to you, sentdex, techwithtim and Dennis Ivy I now know how to properly code on python, use git for version control and build a functional website. - How to Use ChatGPT as a Powerful Tool for Programming

You almost had me there haha! ChatGPT can be wrong but it certainly wasn't wrong about those top notch resources! I massively appreciate all the uploads and I hope they keep coming supercharged with Chat GPT. Thanks to you, sentdex, techwithtim and Dennis Ivy I now know how to properly code on python, use git for version control and build a functional website.

How to Use ChatGPT as a Powerful Tool for Programming
2023年05月22日  Uj Vag 様 
00:28:42 - 00:31:08
and it's  am here (south east asia), and idk why I'm watching a python tutorial rather than sleeping 😂 - Python Tutorial: Simulate the Powerball Lottery Using Python

and it's am here (south east asia), and idk why I'm watching a python tutorial rather than sleeping 😂

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  X 様 
00:02:49 - 00:38:56
… nice… 😂 - Python Tutorial: Simulate the Powerball Lottery Using Python

… nice… 😂

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  Bartosz Turkowyd 様 
00:04:50 - 00:38:56
I've seen every Python video you've made....   first innuendo. 😜 - Python Tutorial: Simulate the Powerball Lottery Using Python

I've seen every Python video you've made.... first innuendo. 😜

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  Martin Beaudry 様 
00:04:52 - 00:38:56
Man it's  am in India I am lucky to be the first one to hear your voice god bless you brother and an awesome teacher. - Python Tutorial: Simulate the Powerball Lottery Using Python

Man it's am in India I am lucky to be the first one to hear your voice god bless you brother and an awesome teacher.

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  Big Samosa 様 
00:12:15 - 00:38:56
# you can also use the built-in pprint module for pretty printing dictionaries ;) - Python Tutorial: Simulate the Powerball Lottery Using Python

# you can also use the built-in pprint module for pretty printing dictionaries ;)

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  Schoenling Wunderbar 様 
00:25:00 - 00:38:56
it would be very interesting to see if there could be a step function where this algorithm can simulate the increases in jackpot winnings. It’s going to be one in 292+ million of a chance that you hit it at all much less multiple times, but the code should be interesting to see. - Python Tutorial: Simulate the Powerball Lottery Using Python

it would be very interesting to see if there could be a step function where this algorithm can simulate the increases in jackpot winnings. It’s going to be one in 292+ million of a chance that you hit it at all much less multiple times, but the code should be interesting to see.

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  20 80 様 
00:27:43 - 00:38:56
A OAuth Django video would be awesome!(Applicable to all sorts of OAuth situations, not just the YouTube API.) - Python YouTube API Tutorial: Using OAuth to Access User Accounts

A OAuth Django video would be awesome!(Applicable to all sorts of OAuth situations, not just the YouTube API.)

Python YouTube API Tutorial: Using OAuth to Access User Accounts
2020年09月10日 
00:07:58 - 00:43:21
At  you move the file and see some blurred miniature version of your open folders in the dock. How did you enable that? - Python YouTube API Tutorial: Using OAuth to Access User Accounts

At you move the file and see some blurred miniature version of your open folders in the dock. How did you enable that?

Python YouTube API Tutorial: Using OAuth to Access User Accounts
2020年09月10日 
00:09:58 - 00:43:21
In case you are getting Error 403 ("access_denied The developer hasn’t given you access to this app") go to the OAuth consent screen and under Test Users add your email by clicking on Add Users.  You should be able to grant access after you've added User. - Python YouTube API Tutorial: Using OAuth to Access User Accounts

In case you are getting Error 403 ("access_denied The developer hasn’t given you access to this app") go to the OAuth consent screen and under Test Users add your email by clicking on Add Users. You should be able to grant access after you've added User.

Python YouTube API Tutorial: Using OAuth to Access User Accounts
2020年09月10日 
00:19:36 - 00:43:21
@corey  So why did you add the join function here, when appending the video id to the list its already a list and its working for me when I pass the vid_ids directly - Python YouTube API Tutorial: Calculating the Duration of a Playlist

@corey So why did you add the join function here, when appending the video id to the list its already a list and its working for me when I pass the vid_ids directly

Python YouTube API Tutorial: Calculating the Duration of a Playlist
2020年06月11日 
00:11:24 - 00:37:38
TypeError: expected string or bytes-like object, I am taking this error in , any idea? - Python YouTube API Tutorial: Calculating the Duration of a Playlist

TypeError: expected string or bytes-like object, I am taking this error in , any idea?

Python YouTube API Tutorial: Calculating the Duration of a Playlist
2020年06月11日 
00:18:50 - 00:37:38
(I love that "Whoops that went too well"-moment at  :D) - Python Tutorial: Real World Example - Using Patreon API and Pillow to Automate Image Creation

(I love that "Whoops that went too well"-moment at :D)

Python Tutorial: Real World Example - Using Patreon API and Pillow to Automate Image Creation
2020年05月11日 
00:35:19 - 00:52:33