object references
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: object references
Felipe Almeida Lessa schrieb: > Em Sáb, 2006-03-25 às 21:33 -0800, DrConti escreveu: > [snip] > > There was also a suggestion to write a real problem where referencing > > is really needed. > > I have one...: > [snap] > > There are loads of discussions about the code you wrote... but... isn't > bad practice to put the same data in two places? Or am I missing > something? > > Cheers, > > -- > Felipe. Hi Felipe, surely it's bad practice to put the same data in two places. However I don't want to put the data in the identifier list, but just the reference to the attributes. My general problem is to find a way to define subsets of instance attributes (for example the identifier), so that at later time I can just iterate over the subset. In the meantime I found a 90% solution to the problem through lambdas.. See now the code below: maybe you'll understand my point better. Thanks and Regs, Davide class ObjectClass: """ Test primary Key assignment """ def alias(self,key): return lambda: self.__dict__[key] def __init__(self): self.Identifier=[] def getPK(self): return [ GetPKValue() for GetPKValue in self.Identifier ] if __name__ == "__main__": ObjectClassInstantiated=ObjectClass() ObjectClassInstantiated.AnAttribute='First PK Elem' ObjectClassInstantiated.AnotherOne='Second PK Elem' ObjectClassInstantiated.Identifier.append(ObjectClassInstantiated.alias('AnAttribute')) ObjectClassInstantiated.Identifier.append(ObjectClassInstantiated.alias('AnotherOne')) print ObjectClassInstantiated.getPK() ObjectClassInstantiated.AnAttribute='First PK Elem Changed' print ObjectClassInstantiated.getPK() >./test.py ['First PK Elem', 'Second PK Elem'] ['First PK Elem Changed', 'Second PK Elem'] --> correct now! -- http://mail.python.org/mailman/listinfo/python-list
Re: object references
Hi Bruno, hi folks! thank you very much for your advices. I didn't know about the property function. I learned also quite a lot now about "references". Ok everything is a reference but you can't get a reference of a reference... I saw a lot of variations on how to solve this problem, but I find actually that the "property approach" is the most natural of all. Basically the idea there is that you build up this list of class attributes not by storing a reference to a class attribute (which seem to be impossible), but you just store on each element of the list one method (pardon a reference to..) to get the associated class attribute. Sorry for the UnPythonity. I used to be a CamelRider. But not very longtime ago I left the Camel in the Desert, because I met the Snake Regs, Davide. -- http://mail.python.org/mailman/listinfo/python-list