object references

Scott David Daniels scott.daniels at acm.org
Mon Mar 27 21:05:52 CEST 2006

DrConti wrote:
> I need a variable alias ( what in other languages you would call  "a
> pointer" (c) or "a reference" (perl))
Or, you think you need it.

> 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.
Generally, yes.  The line goes, roughly, "You've decided on a solution
and are twisting your problem to fit it."

> 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).

First, this is _not_ a "real problem"; this is a bunch of code.  The
"real problem" request is to provide an actual use case, not some code
where you want to write what you want to write.

> 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

If you insist on this kind of approach, you could use a pair of
an object, and an attribute name as a "reference," and use getattr
and setattr to access the identified attribute.  _But__ I emphasize
that you are thinking about your problem from the point of view
of a solution, not from the point of view of the problem.

You'd probably like this:

     class Example(object):
         """ Test primary Key assignment """

         def __init__(self, one, two, other):
             self.one = one
             self.two = two
             self.other = other

         def __repr__(self):
             return '%s(%r, %r, %r)' % (
                 type(self).__name__, self.one, self.two, self.other)

     if __name__ == "__main__":
         eg = Example(3.1415, 3+4j, 'pi')
         ref_attr = eg, 'one'
         ref_other = eg, 'other'
         print eg, getattr(*ref_attr), getattr(*ref_other)
         eg.one = 'First PK Elem'
         print eg, getattr(*ref_attr), getattr(*ref_other)
         setattr(*ref_other + (u'Strangely',))
         print eg, getattr(*ref_attr), getattr(*ref_other)

But you might consider this:

     class Another(Example):
         """ Test primary Key assignment """
         key = ('one', 'two')

     def getkey(v):
         return [getattr(v, part) for part in v.key]

     if __name__ == "__main__":
         eg2 = Another(3.1415, 3+4j, 'pi')
         print eg2, getkey(eg2)
         eg2.one = 'First PK Elem'
         print eg2, getkey(eg2)
         setattr(eg2, 'two', u'Strangely')
         print eg2, getkey(eg2)

-Scott David Daniels
scott.daniels at acm.org

More information about the Python-list mailing list