Skip to content Skip to sidebar Skip to footer

How To Specify A Variable As A Member Variables Of A Class Or Of An Instance Of The Class?

in latest Python 2.7.x: Given any member variable inside the definition of a class, is the member variable always at the class level in the sense that it is a single variable shar

Solution 1:

You might want to use the terminology "class variable" and "instance variable" here, as that's the usual language in python.

classFoo(object):

    var1 = "I'm a class variable"def__init__(self, var2):
        self.var2 = var2  # var2 is an instance variable

The only scoping rule you really need to know in python is the lookup order for names - "LEGB", for Local, Enclosing, Global and Builtin.

The class scoped variable var1 still has to be looked up by "get attribute", you can only access that by Foo.var1 or self.var1. Of course, you can also access it elsewhere inside the class definition block, but that is just an example usage from the "Local" scope.

When you see self.var1, you can't immediately know whether it is an instance or a class variable (nor, in fact, if the name is bound to an object at all!). You only know that get attribute is tried on the object itself before it's tried on the class.

Indeed, an instance variable can shadow a class variable of the same name:

>>>f1 = Foo(var2='f1_2')>>>f2 = Foo(var2='f2_2')>>>f2.var1
"I'm a class variable"
>>>f2.var1 = "Boom!"# this shadows the class variable>>>f1.var1, Foo.var1, f2.var1  # but: the class variable still exists
("I'm a class variable", "I'm a class variable", 'Boom!')
>>>del f2.var1  # restores the name resolution on the Foo object>>>f2.var1
"I'm a class variable"

To complicate matters, we can write fancy code which makes class variables behave more like instance variables; a notable example are "fields" of an ORM. For example in Django, you may define an integer field on the model class - however when you lookup that name on an instance of the model, you get an actual integer returned (not an IntegerField object).

If you're interested in this advanced usage of attribute access, read up on the descriptor protocol. For mundane classes you can safely ignore those details, but it's worth knowing that the usual resolution of instance variables and then class variables has lower precedence than any descriptors that may have been defined on the class.

Post a Comment for "How To Specify A Variable As A Member Variables Of A Class Or Of An Instance Of The Class?"