Comment about Class vs Instance Variables video

+1 Vadim Kotlarov · August 18, 2015

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.

Post a Reply


Oldest  Newest  Rating
0 Vadim Kotlarov · August 20, 2015
The thing is, class variables and instance variables don't have to be defined where you wrote them.

For example:
class Girl:


a = Girl()

print(          #Output would be Rachel
print(a.gender)        #Output would be female
print(       #Output would be Amy
print(          #Output would be Amy

This would put the gender variable as a class variable and the name variable as well.
Until the point I decide to change 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 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.
0 Halcyon Abraham Ramirez · August 18, 2015
so basically class variables/class attributes are variables within you class i.e

class Hey:

    class_variable = "this"

while instance variables are the variables you set to your __init__ right?

class Hey
    class_variable = "this"
    def __init__(self,this_is_an_instance_variable):
        self.this_is_an_instance_variable = this_is_an_instance_variable

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

list1 = [1,2]
a = list1


compare the output above to this:

list1 = [1,2]
list_copy = list(list1)
b = list_copy


it's kinda similar to our analogy of being the same but are actually unique or to better phrase it. Independent of each other
  • 1



This section is all about snakes! Just kidding.

Bucky Roberts Administrator