If you want to change the class variable, do:
class test(object): d=0 def __init__(self): type(self).d=self.d+1; D=test() print Dd D1=test() print D1.d D2=test() print D2.d
You do not need type on the right side of the destination, because in this way you will never create an instance variable d . Please note that this requires new-style classes.
type is a function (actually called - it is also a class, but don't worry about it at the moment) that returns the class of its argument. So type(self) returns the class self . Classes are first class objects in Python.
Demo here: http://ideone.com/JdNpiV
Update: An alternative would be to use classmethod .
source share