My code:
>>> class Class1:
pass
>>> obj1=Class1()
>>> obj2=Class1()
>>> obj1.x1=123
>>> obj2.x2=456
Then I got the following errors:
>>> obj1.x2
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
obj1.x2
AttributeError: Class1 instance has no attribute 'x2'
And similarly:
>>> obj2.x1
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
obj2.x1
AttributeError: Class1 instance has no attribute 'x1'
The AttributeError is quite strange, why it says the Class1 has no attribute 'x1' and 'x2'? Python claims to be able to add fields on the fly. And I am expecting result like this:
obj1.x2 = None
obj2.x1 = None
What's the difference between the field add on the fly and the contained in the class definition?
obj1andobj2?obj1 = Class1()in your example?Class2?obj1.x2causes an error becauseobj1has nox2attribute -- you added that attribute to theobj2instance ofclass Class1, which does not automatically add it to the existingobj1instance.