Python OOP Tutorial 2: Class Variables

1,712,809
0
Published 2016-06-23
In this Python Object-Oriented Tutorial, we will be learning about class variables. We will see how they differ from instance variables and also some ideas for exactly how we would want to use them. Let's get started.

Python OOP 1 - Classes and Instances -    • Python OOP Tutorial 1: Classes and In...  
Python OOP 2 - Class Variables -    • Python OOP Tutorial 2: Class Variables  
Python OOP 3 - Classmethods and Staticmethods -    • Python OOP Tutorial 3: classmethods a...  
Python OOP 4 - Inheritance -    • Python OOP Tutorial 4: Inheritance - ...  
Python OOP 5 - Special (Magic/Dunder) Methods -    • Python OOP Tutorial 5: Special (Magic...  
Python OOP 6 - Property Decorators -    • Python OOP Tutorial 6: Property Decor...  

The code from this video can be found at:
github.com/CoreyMSchafer/code_snippets/tree/master…


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

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

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

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

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

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

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

#Python

All Comments (21)
  • @mtvcity2
    Your videos are more informative than my university lectures
  • it really makes a difference who teaches you.....it was wtf wtf wtf....then watch these videos...oh ya that's logical. you are an awesome teacher....please keep the videos coming.
  • @user-xc5xf8gp8n
    Summary: In this video, Corey taught as how to differentiate between a Class variable and instance variable, how they are related to each, other, and when each of them is more useful over the other. Class variables are variables that we set inside a class, and are shared among all instances. Corey gave a good example where the number of total employs should be the same to every employ, no matter which employee we are referring to. Therefore, emp_1.num_of_employ = emp_2.num_of_employ = Employee.num_of_employ Instance variables are variables that are different from each instance. For example, the names and the pay for each employee. They have to be different. Corey also shows that class variables and instance variables are closely related, and that class variables are kind of 'inherited' to the 'self' variables. To illustrate this, Corey shows an example of 'annual raise of pay'. He initially creates the class variable to show a case where annual raise is equal among all the employees. This variable of 1.04 was accessible through each instance, and also through the class itself(obiviously). That is, print(Employee.annual_raise) print(emp_1.annual_raise) print(emp_2.anual_rais) all printed out 1.04. However, using the ._dict__ thing, Corey shows that the intances, emp_1 and emp_2 does not contain the annual_raise value. Corey explains that if a variable is not found within an instance and programmers try to access the variable, python automatically looks in in the variable of the instance's class, and then the more classes that the instance's class inherits from. Furthermore, if we access the class variable through an instance and then change it, python creates the variable within the instance. We can check it by using the ._dict_ thing. Corey shows that annual_raise key was created when he manually changed the annual_raise value as 1.05 in the following way. emp_1.annual_raise = 1.05 however, we know that the class variable remained the same at 1.04, when printing the class variable. print(Employee.annual_raise) ==> 1.04
  • I enrolled into a top seller's python course on Udemy, read from some of the famous websites but I didn't find someone who teaches like you.You are, hands down, the best teacher anyone can have.
  • @engee_346
    The sleepless hours that I spent trying understand the nomenclature and the functions that he just made clear in 11 minutes... if you had an advanced professional class that cost money, I would pay for it.
  • This is so good that I'm starting to doubt whether all his 'mistakes' are secretly ways to make us learn faster :P
  • @wonderoverload0
    Who needs a Udemy course when you have Corey on YouTube. Thanks buddy for making internet useful for us.
  • @roro4787
    I finally understand the difference between teaching and throwing information. Love your explanation Corey!
  • @nikolahuang1919
    This series is the best Python series I've ever watched. Your explanation is so clear and you just teach the right amount of useful knowledge we need.
  • @takuz7879
    6 years later, and your videos are still helping us students. I'm in highschool and I'm literally studying for my exam through these. When I pass it'll all be because of you. THANK YOU COREY!
  • You have the best python OOP tutorials. Thank you, you are helping alot of people especially aspiring developers from Africa. Cheers
  • @mccallionr
    These videos are insanely good... Definitely not for a 100% noob but absolutely amazing for somebody who's hacked around and now needs a technical understanding to get to the next level. Thanks, Corey!
  • @peti826
    I know there loads of comments appreciating your work already, but man.. . I started learning python about 1,5 months ago and I have never learnt about OOP, not even its concept and the difference related to other types (still dunno them properly), yet I fully understood everything you said in both of your videos so far... Hats off dude.
  • @falconspy1668
    The best python OOP tutorial on YouTube. Thank you sir
  • I don't know why you don't have more subscribers by now, or maybe there aren't many coders out there. But I was afraid of starting learning Python until I stumbled onto this Chanel. Now I can code as a charm.
  • So, 5 years ago, this amazing video was made and I'm just seeing it in 2022! What a classic teaching it is! Thank you for the supremely brilliant delivery 👍
  • @Bartnick81
    Quality tutorial! You got my patreon backing.
  • What I love about these lecture, that Errors are explained, which is more intuitive. Thanks Corey :)
  • @harshhes
    OOPs tutorial couldn't have been more better than this.Respect from india, Mr.schafer.