DrConti wrote: > class ObjectClass: > """ Test primary Key assignment """ > > if __name__ == "__main__": > ObjectClassInstantiated=ObjectClass() > ObjectClassInstantiated.AnAttribute='First PK Elem' > ObjectClassInstantiated.AnotherOne='Second PK Elem' > ObjectClassInstantiated.Identifier=[] > > ObjectClassInstantiated.Identifier.append(ObjectClassInstantiated.AnAttribute) > > ObjectClassInstantiated.Identifier.append(ObjectClassInstantiated.AnotherOne) > print ObjectClassInstantiated.Identifier > ObjectClassInstantiated.AnAttribute='First PK Elem Changed' > print ObjectClassInstantiated.Identifier > > leads a wrong result >> ./test.py > ['First PK Elem', 'Second PK Elem'] > ['First PK Elem', 'Second PK Elem'] > --> wrong! It should write ['First PK Elem Changed', 'Second PK Elem'] > > i.e. the assgnment > ObjectClassInstantiated.Identifier.append(ObjectClassInstantiated.AnAttribute) > assigns only the attribute value, not the reference.
Nono, it assigns the reference alright. In python, EVERYTHING gets assigned only a reference, .AnAttribute as well. So when you do .AnAttribute = 'Changed', you make it a reference to a NEW string 'Changed', while .Identifier[0] keeps referencing the 'First PK' string object. Strings are an unfortunate example, since they're immutable - once you create a string object, you cant't modify it any more. But if you had a more complex object, you could do .AnAttribute.changeYourself(), and .Identifier[0] would change accordingly as well, because .AnAttribute would keep pointing to the same object (albeit changed). In your case, try .AnAttribute = ['First']; .Identifier[0] = .AnAttribute; .AnAttribute[0] = 'First changed'; - this will work the way you want it to, because .AnAttribute doesn't get rebound (only the content of the object (list) it's pointing to change, but it's still the same object). -- http://mail.python.org/mailman/listinfo/python-list