Python OOP Tutorial 2: Class Variables
Vložit
- čas přidán 26. 07. 2024
- 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:...
Python OOP 2 - Class Variables - • Python OOP Tutorial 2:...
Python OOP 3 - Classmethods and Staticmethods - • Python OOP Tutorial 3:...
Python OOP 4 - Inheritance - • Python OOP Tutorial 4:...
Python OOP 5 - Special (Magic/Dunder) Methods - • Python OOP Tutorial 5:...
Python OOP 6 - Property Decorators - • Python OOP Tutorial 6:...
The code from this video can be found at:
github.com/CoreyMSchafer/code...
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ 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 - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python
Your videos are more informative than my university lectures
Thanks a lot! That's a nice compliment. I'm glad to hear you find them useful.
and short and arent booooooring
This is so true. We obtain more information on those really short videos than we do in those long lectures on universities.
True Story of every university
Well, kind of. In your videos we learn basically all the useful stuff we want to know, but in university we get all the side-information (sorry, no native speaker) we may find useful someday, even though we don't know yet, when it could happen.
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.
Indeed - a "GOOD" teacher is PRICELESS !!!!!!
I noticed he uses a lot of printing to explain things. That's why it seems logical. When in doubt, just print it out!
well said, a teacher can kill or instill in you a love for a subject
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
Thank you so much. I wanted to revise everything he said but did not want to watch the entire video again. Your comment helped me with just.
in the second para you said that class variables are shared among all instances but I dont think thats the case. Corey said pyhton first searches the variables in the instance but if it doesnt find it, it goes to the class of the instance and returns it from their. if you dont explicitly mention emp1.raise_amount = 1.05, emp1 instance doesnt have the raise_amount vriables. Please confirm
@@amansinghal2431 I think he means, when the raise_amount variable isn't found in the instances, python searches for the instance in the class and applies it to both the instances, thus making them share the same variable. I hope I was clear
Thankx a lot mate
@@amansinghal2431 The fact that class variables are shared among instances is correct. When class variables are used in methods using class names (as in Employee.raise_amount), then python directly accesses the class namespace, which is common for every instance. But when self is used (as in self.raise_amount), then python first searches the object's namespace, and if not found, then the class namespace. That's why Corey states that using self allows to assign different class variable values to different objects, as reassigning them (such as self.raise_amount = 1.05), makes the value a part of the object's namespace which is queried first.
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.
which course did you bought from udemu=y,can i know
i made the same mistake
me too
Which Course did you buy .?
@@ayeshavlogsfun I bought all. All of them below par compared to this.
This is so good that I'm starting to doubt whether all his 'mistakes' are secretly ways to make us learn faster :P
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.
Who needs a Udemy course when you have Corey on CZcams.
Thanks buddy for making internet useful for us.
I finally understand the difference between teaching and throwing information. Love your explanation Corey!
throwing information 😂😂😂
Underrated comment
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.
Thanks!
可不是咋的!
You have the best python OOP tutorials. Thank you, you are helping alot of people especially aspiring developers from
Africa. Cheers
That's great to hear! Glad you found it helpful!
Actually from the same country bro
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!
did you pass?
@@eliasvor yeah 🙌
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.
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!
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.
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.
I'm so so so glad that I found Corey on CZcams. 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!
Thanks! Glad to have you
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 👍
you helped me get an internship. :*
Congratulations!
@@robbyz512 for experience and understanding , thats simple even tho i wasnt in any internship :p you are basically learning how to work in that level
@@robbyz512 i think you are paid man not like alot but still
@Berke Icel oh still also a good way for experience :3
@Berke Icel That's not entirely true (not being payed for internships). I actually was paid for an internship I did before I was hired on after it was over. I know for a fact the big companies like Facebook, Microsoft, Google, Twitter, etc. pay interns via an educational stipend.
It's great that there are people with pedagogical grace, but it's really wonderful when they share their knowledge with beginners. Please don't stop what you're doing ❤
hands down the best OOP tutorial series... I've been using python for 2 years, but have been intimidated by Classes.... this has helped a ton!!!!!
Quality tutorial! You got my patreon backing.
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!
What I love about these lecture, that Errors are explained, which is more intuitive.
Thanks Corey :)
Best Python OOP lessons for beginners!
As a beginner, I was stuck with learning Python Class and so lucky to find these videos via Google search! Very informative and easy to understand. BTW, the teacher in these videos also has a nice voice ☺
I read a ton of explanation for class attributes and instance attributes, but you explain it so clearly. Great job!
Showing the namespace was so clever to help us thoroughly understand it. Thank you so very much!
As a CS person myself, I’ve seen numerous tutorial videos. Yours is the most logical, right to the point, and easy to understand. 👍 Hope you’ll create more contents.
Corey your explanation of "Class" is just that...... CLASS. Perfect explanation!!! Total Respect.
I've seen several sites, books, and videos that all used the Employee class as an example for OOP, however this is the only one that actually made sense and helped me understand the concepts. Thanks!
Corey, your videos are EXEMPLARY :) Your approach to teaching, and examples you use (e.g. Employees), have finally unlocked the hard-to-understand concepts into easier-to-understand analogies. Thank you for posting these videos!
This is just pure gold. That's how any teacher should be , who makes their student eager to learn more in most simplistic way. Great work Corey.
Your pretty much the only thing and/or person who explains any of this clearly
Hi, I need to say your videos are really clear, comprehensive, enough detailed but not boring and too long on the other hand. I just sent you some small donate right now as appreciation. I wish I discovered your channel sooner. Thank you for all your effort!
I always had difficulties understanding Python class well, until I found this video that makes it so intuitive.
Mind blown..!!! 20+ years ago I was working with Macromedia (Macromind) Director and as a visual designer without coding knowledge or experience I struggled to master Lingo. Over time I ended up at the level of an OOP Lingo coder, albeit one relying heavily on copied code segments and fiercely treasure code examples that I'd gotten to work. FF 20 years and I've returned to coding in the form of Python.
SUDDENLY with your videos a window has opened on the murky interior I previously inhabited. I now understand the concepts that you are talking about rather than piecing together code samples. ALL of your videos that I have worked through have been enlightening, fun and edifying. Thank you so much for giving all of this.
Oh My God. I was struggling with OOP concepts. Corey made it so simple. Corey keep on doing what you doing my good friend. Thank you.
Hello Corey, I'd like to thank you for all the courses on Python you've made. They're way better than udemy courses and any others. You're doing a great job, captain!
The best python OOP tutorial on CZcams. Thank you sir
You, sentdex, Traversy Media are the teachers we all need in Uni but don't have. The day I get a job, I'll definitely support you. Thank you for sharing your knowledge.
After working in the industry for 5 years, learned this concept today, just because of the way you made it. I am moving towards into a more coding technology and your videos are gold! Amazing!
Really? 5 years of tech support scams, and yet you don't know any basics of OOP. Now that's some top tier career dreams
I've been watching several python training videos. Corey explains things more simply and more completely than anything I have watched so far. Good work.
Thanks! Glad it helped!
These are the best videos ever . I struggled to make sense of these concepts before . Now its all so damn logical and intresrting. A Million tonnes of gratitude to u !
OOPs tutorial couldn't have been more better than this.Respect from india, Mr.schafer.
I have NOTHING BUT PRAISES for your style, clarity, and to the point explanation of VERY IMPORTANT concepts .... I cannot thank you enough Corey .. I will gladly contribute to your classes and start filling all the "gaps" of knowledge I need to .. You have a devoted student now :)
I was thinking i am very good in OOP since got i A+(4.0) in the course both in Theory & Lab, somehow i came to the playlist & saw pretty amazing comments, so i got interest to see what's inside. i was thinking myself a pro in OOP. But the way you teach things here are amazing.
You start with messing up the things, then collaborate them and make a great sense. Awesome.
Just want to say thank you for these videos. I took the wrong path in my education before realising that programming was my true passion. Unfortunately its really difficult to self-teach something like this because of the sheer wealth of information avaliable online.
Your videos cover almost everything I've wanted to learn so far, and not only that you don't just 'show' you actually teach. Every time i find myself asking a question about something you've just said, you immediately address why it and explain why it may be confusing. Thank you so much for putting in the time to do this and make it free, you are really making a difference to peoples lives.
Lol your explanations are better than all my professors. How did I not find this channel years ago smh
I get more explanations from your videos than what I have got from all of my courses, great work!!!!!!
Going through your videos we feel like we have covered the most. What we can gain by going through multiple books you walk us through it in one video. Thank you Corey from the bottom of my heart.
You are an outstanding teacher, showing the reasoning behind things is MASSIVELY impactful. Feels like my brain is expanding watching your videos lol
Absolutely freaking awesome. Hands down, you're providing me an A+ headstart for my bachelor course in economy informatics. Huge thumbs up for spreading absolutely logic and understandable explanations. You're amazing. Thanks.
This is the type of youtuber whom I would gladly watch 10 straight ads on their video
I know most of the other comments have expressed the same feelings a year or two before me, but these are the best tutorials for object oriented programming in python that I've found. It 100% cleared things up for me. I feel like I can finally move on!
This. absolutely blown away in the difference of these tutorials. He has a gift to teach.
As somebody who used C# for years, and is now picking up some python. Thank you for clearing up some of what I saw as weirdness with the way python does things. Its all clear now and simple.
Finding your channel is like finding a Gold mine Corey , keep up the Gold work ; - )
BEST PYTHON OOP EXPLANATIONS I HAVE SEEN.
I HAVE BOUGHT MANY ONLINE COURSES BY THEY ARE SUCKS COMPARING TO YOU.
I first started watching these videos to help my son, who is currently taking coding courses. I found your videos very very helpful. Thanks a lot, Corey..
I have been searching for good tutorials on Python for years, and this series is by far is the most informative. Thank you!
This is the best Video ever to explain classes, i have been very lost for a month trying to get this concept and you explained it in less than an hour thank you soo much. I am going to nail my computer science paper tomorrow
your videos are much better compared to many online courses and free too
What I loved about these tutorials is that he tried to cover those tricky questions such as instance namespace and class namespace ever for people that are already familiar with these concepts. Keep making videos. Your content is great!
Your videos should be at top in the youtube search for "python". I'm recommending your channel to all my friends interested in Python. Thanks Corey. You are a great teacher.
Tank you for covering all of this. I'm truly becoming better in Python because of this
Thanks for making a concise and understandable lesson on 'self'. I had one semester on python and my professor could never really make it make sense and you managed to explain it is like 20 minutes. Heck yeah!
I can see them asking at interviews whether or not you've used the 'tutorials provided by Corey Schafer on CZcams'. You are a natural teacher my friend! Gratz again on the 1mil! Should definitely be well over that! If you don't get your next plaque, make sure to contact YT, apparently they 'forget' sometimes =P
Senior here and taking a different online course just to keep the synapses working. Well, you have it way over them in teaching quality. Wish I had seen you first. Thank you.
This is the most logical approach to class and objects, i am really glad i found you Sir! Thankyou for such a beautiful explanation. You solved every doubt!
can't express how nice and easy to understand these videos are
The best Python tutorials I have come across. Your explanation is just awesome. Keep up the good work.
You are so thorough! I agree with others that you are a natural teacher. The pace, tone, comprehensiveness, and genuine enthusiasm are all spot-on. You are an asset! Thank you!
You might have no idea how helpful this was to us.
Best CZcams channel there is! Bravo!
This is so .... good I am so lucky to stumble up on this. I bought a udemy course and it got me nowhere in 6 hours . I just watched 24-30 minutes of this and I totally get it now . Thank you so much you are an amazing guy
The best explanation on Python Classes on YT. Congrats, man
1 hour of my Grad class summed up in 11 minutes. Thank you!
Thank you so much for this series, these videos are fantastic, I feel like every possible pitfall or misunderstanding one could have you articulately and expertly explain.
Posted 4 years back but still, I did not find any other tutorial as clear as these classes one. I have just started learning python and I really appreciate you making these videos. You are an awesome teacher.
Being from a non-technical background, I was struggling to understand the OOP. But now the concept is clear to me. Thank You..
Dude, these tutorials are mindblowing.. I've gone through a few OOP classes and never really understood the concept so i can write my own things with it, after watching tutorial 1 and 2 i now have a very good understanding how to rewrite a whole script i got. Thanks, i never sub but this deserves a sub.
Glad it helped!
Who gives dislikes for gems like these??? For now I only have love and respect for you Corey, Patreon to follow soon :)
"Okay, and so why did it do that?" My favorite words xD, thank you so much for going further in explanations and usage.
Everything you explained is clear and logical. Thank you
This is so much better than my lectures and book, thank you so much!
After purchasing a few tutorial classes going through the online material this is where i got the bestsellers explanation. #Thanks Corey Schafer #Respect
Don't know how to express how grateful I am to have all these materials. Thank you a lot Corey.
All these comments are spot on. I just came from another very popular python instructor on youtube and he skipped over the fact python going to the class attribute if it doesn't find it on the instance of the class. Might be in the paid tutorial he says you can buy if you want to learn more lol!
Everything is so well placed and informative, thank you!
You are literally giving me hope, appreciate it
I tried watching these videos several months ago... all went over my head. Now the lightbulbs just keep popping!!
Bow down to the maestro...the way you teach is still the best after 5 years of this video...I have been to lots of tutorials for python but always missing the concept...you made it like a butter in the frying pan...it just slips through your mind and I feel satisfied after learning from your videos.... please don't stop your videos...the world needs teacher like you ... #respect
Corey, to repeat the sentiment here, you really helped drive home what Object Oriented Programming almost instantly. You should have your own Coursera courses.
You are next level, man. Respect!
Probably the best videos Ive been able to find for beginner Python. So articulate and logical in the way you teach, it's awesome. Thanks.
I could cry. I wrote a program that needed to automaticly create Buttons and I sat down there for like 5 hours to do this but now I can easily make the same program in 5 minutes and a third of the code. Thank you so much!!
Every video is so clear thanks bro!
I've been using Sololearn, but this is more straightforward and easy to understand. Thanks!
I am using Sololearn as well and Corey is way better at the OPP stuff!
Dude I miss your videos. Best python videos on youtube. I hope you come back and start uploading more valuable videos.
You helped me finally understand object oriented programming in just one video after hours of surfing the web without any result.