Class instance value setting : Class Definition « Class « Python






Class instance value setting

 

class Foo :
   x = 1


f1 = Foo()
f2 = Foo()
print f1.x
print f2.x
f1.__class__.__dict__['x'] = 2
print f1.x
print f2.x

f3 = Foo()
print f3.x
f3.x = 3
print f2.x
print f3.x

if f2.x == f2.__class__.__dict__['x'] :
    print "No change"
if f3.x == f3.__class__.__dict__['x'] :
       print "No change"
else :
       print "Value changed"

   
  








Related examples in the same category

1.Class Time with properties
2.Class Time with customized attribute access.
3.Simple class with slots
4.Definition and test function for class Point.
5.User-Defined Classes
6.NameTag class
7.Define a class