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__":
ObjectClassInst antiated=Object Class()
ObjectClassInst antiated.AnAttr ibute='First PK Elem'
ObjectClassInst antiated.Anothe rOne='Second PK Elem'
ObjectClassInst antiated.Identi fier=[]
ObjectClassInst antiated.Identi fier.append(Obj ectClassInstant iated.AnAttribu te)
ObjectClassInst antiated.Identi fier.append(Obj ectClassInstant iated.AnotherOn e)
print ObjectClassInst antiated.Identi fier
ObjectClassInst antiated.AnAttr ibute='First PK Elem Changed'
print ObjectClassInst antiated.Identi fier
leads a wrong result[color=blue]
>./test.py[/color]
['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
ObjectClassInst antiated.Identi fier.append(Obj ectClassInstant iated.AnAttribu te)
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
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__":
ObjectClassInst antiated=Object Class()
ObjectClassInst antiated.AnAttr ibute='First PK Elem'
ObjectClassInst antiated.Anothe rOne='Second PK Elem'
ObjectClassInst antiated.Identi fier=[]
ObjectClassInst antiated.Identi fier.append(Obj ectClassInstant iated.AnAttribu te)
ObjectClassInst antiated.Identi fier.append(Obj ectClassInstant iated.AnotherOn e)
print ObjectClassInst antiated.Identi fier
ObjectClassInst antiated.AnAttr ibute='First PK Elem Changed'
print ObjectClassInst antiated.Identi fier
leads a wrong result[color=blue]
>./test.py[/color]
['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
ObjectClassInst antiated.Identi fier.append(Obj ectClassInstant iated.AnAttribu te)
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
Comment