Python OOP Tutorial 2: Class Variables

preview_player
Показать описание
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.

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

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

✅ Corey's Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:

#Python
Рекомендации по теме
Комментарии
Автор

Your videos are more informative than my university lectures

mtvcity
Автор

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

xcxfgpn
Автор

it really makes a difference who teaches was wtf wtf wtf....then watch these videos...oh ya that's logical. you are an awesome teacher....please keep the videos coming.

trevorduncan
Автор

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.

HaroonAshrafAwan
Автор

Who needs a Udemy course when you have Corey on YouTube.
Thanks buddy for making internet useful for us.

wonderoverload
Автор

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.

engee_
Автор

This is so good that I'm starting to doubt whether all his 'mistakes' are secretly ways to make us learn faster :P

charlesdeleau
Автор

You have the best python OOP tutorials. Thank you, you are helping alot of people especially aspiring developers from
Africa. Cheers

derricknjoroge
Автор

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.

nikolahuang
Автор

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!

takuz
Автор

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.

peti
Автор

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!

thuggfrogg
Автор

I'm so so so glad that I found Corey on YouTube. His teachings are state of the art! Whenever there's something I'm confused about python, he is the first person that comes in mind. I'm so very grateful to have you as a teacher, you rock at it! Keep it up! Can't wait for more!

ravenjs
Автор

Wow! Learned more about classes and class variables, in the last 10 minutes, than on a full day going through online python CBT. Can't wait to watch the rest of this series.

jaxwylde
Автор

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 👍

taofeekajibade
Автор

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.

willylukwago
Автор

Your pretty much the only thing and/or person who explains any of this clearly

hashtaggamer
Автор

On the 2nd of Feb 2020, this video had a 99.64% like - dislike ratio. That's insane. It stands as a testimony to the insanely great work Corey's been doing. Thank you!

carljason
Автор

What I love about these lecture, that Errors are explained, which is more intuitive.
Thanks Corey :)

SourabhDesaiBrief
Автор

Corey your explanation of "Class" is just CLASS. Perfect explanation!!! Total Respect.

colinsmith