object references
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Sun Mar 26 17:13:53 EST 2006
DrConti a écrit :
> 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))
Well, that's the only kind of "variable"[1] in Python.
[1] the correct name in Python is 'binding', since it's about 'binding'
a reference to a name, not labelling an in-memory address and storing
data there.
> 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...:
You *think* you 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']
Nope, it's exactly what you asked for !-)
>
> i.e. the assgnment
>
> ObjectClassInstantiated.Identifier.append(ObjectClassInstantiated.AnAttribute)
>
> assigns only the attribute value, not the reference.
1/ it's not an assignement
2/ it does not store the attribute "value", it stores the reference to
the object the attribute is bound to. When you later rebind the
attribute, it only impact this binding - there's no reason it should
impact other bindings.
> so my question is:
> is it still true that there is no possibilty to get directly object
> references?
But object references *are* what you have.
> Is there a solution for the problem above ?
Yes : keep a reference to the attribute name, not to the value bound to
that name. There are many ways to do it, here's one:
ObjectClass.Identifier = property(
fget=lambda self: [self.AnAttribute, self.AnotherOne]
)
and here's another one:
ObjectClassInstantiated._identifier_parts = []
# note the use of strings, not symbols
ObjectClassInstantiated._identifier_parts.append("AnAttribute")
ObjectClassInstantiated._identifier_parts.append("AnotherOne")
ObjectClass.Identifier = property(
fget=lambda self: [getattr(self, name) \
for name in self._identifier_parts]
)
> Thank you for any feedback
May I add some ? Your naming conventions are highly unpythonic. We
usually use CamelCase for classes names, and (in order of preference)
all_lower_with_underscores or mixedCaps for
variables/attributes/functions etc.
HTH
More information about the Python-list
mailing list