I might be paying too much attention to small details but in this video Bucky says that the difference between instance and class variables is that one is unique and the other is for the entire class, which is something that isn't completely correct.
While class variables values are the same for all instances, they can be changed to fit the specific instance(They are pretty much a default value).
If I use Bucky's example then r.gender = "male" would work perfectly for r and change it's gender to male, while s would stay female. In this case the class variable changes and becomes an instance variable (There is absolutely no difference where it is written).
The most interesting part about the class variable is that it is just a pointer. If you create 10 instances for class Girl and after that you decide to change gender for it (Girl.gender = ...) then it will change that variable for all the instances, unless some were changed as written previously.
It is also possible to dynamically add class variables by writing Girl.height = 53242 and it will add it to all the instances of the class.
Another point, instance variables are not unique, you can have 10 Girl instances with the name "Rachel" and they would all be in different locations in the memory (And a set would consider them different).
My point is, that there is absolutely no relevance as to where you write the variable.
The difference between a class variable and instance variable is that class variable value is shared by all instances, no matter when it is created and it can be changed when ever needed as it is just a pointed.
And instance variable is a variable that might exist in all the class instances but it can have a different value than others and to change it's value, there is a need to explicitly change it for that specific instance.
Sorry if I might have understood the video wrong but figured I should drop my 2 cents here.
Also, since I am here I would like to thank Bucky for all the videos, very informative.
Comment about Class vs Instance Variables video
Post a Reply
|Oldest Newest Rating|
Halcyon Abraham Ramirez
· August 18, 2015
so basically class variables/class attributes are variables within you class i.e
while instance variables are the variables you set to your __init__ right?
also quite interestingly.
you can have 10 Girl instances with the name "Rachel" and they would all be in different locations in the memory (And a set would consider them different)
by this definition. wouldn't it mean that despite having the 10 different objects with it's instance variable set to Rachel despite being the same aren't they unique to each object? I guess that's what bucky meant by that.also try this
compare the output above to this:
it's kinda similar to our analogy of being the same but are actually unique or to better phrase it. Independent of each other
· August 20, 2015
The thing is, class variables and instance variables don't have to be defined where you wrote them.
This would put the gender variable as a class variable and the name variable as well.
Until the point I decide to change a.name into Rachel, it would also be Amy and only in this point the name variable in a would become an instance variable.
I think the easiest way to explain class and instance variables is by saying that class variable location in memory is shared by all instances of the said class, and can also be accessed using Girl.name. However, once an instance changes a variable with the same name (AKA "name" in the above example), the sharing of that location with that instance is stopped.
Instance variables are pretty much private variables that have their own location in the memory and only that instance can know that location.
This section is all about snakes! Just kidding.
|Bucky Roberts Administrator|