object references
DrConti
daconti.mail at gmail.com
Sun Mar 26 00:33:24 EST 2006
Dear Python developer community,
I'm quite new to Python, so perhaps my question is well known and the
answer too.
I need a variable alias ( what in other languages you would call "a
pointer" (c) or "a reference" (perl))
I read some older mail articles and I found that the offcial position
about that was that variable referencing wasn't implemented because
it's considered bad style.
There was also a suggestion to write a real problem where referencing
is really needed.
I have one...:
I'm trying to generate dynamically class methods which works on
predefined sets of object attributes.
one of these is the set of attributes identfying uniquely the object
(primary key).
A naïve attempt to do the job:
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.
so my question is:
is it still true that there is no possibilty to get directly object
references?
Is there a solution for the problem above ?
Thank you for any feedback and sorry for the long mail
...and the reference to perl :-)
Regs,
Davide
More information about the Python-list
mailing list