object references
bruno at modulix
onurb at xiludom.gro
Mon Mar 27 13:58:21 EST 2006
Steven D'Aprano wrote:
> On Sat, 25 Mar 2006 21:33:24 -0800, DrConti wrote:
>
>
>>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))
>
>
> Others have given you reasons why you can't do this, or shouldn't do this.
> In general, I agree with them -- change your algorithm so you don't
> need indirect references.
>
> But if you can't get away from it, here is another work-around that might
> help:
(snip)
And another one, that mess less with attributes (but more with lookup
rules - use it at your own risks !-):
class CompoundAttribute(object):
def __init__(self, *names):
self._names = names
def __get__(self, obj, objtype):
if obj is None:
return self
return [getattr(obj, name) for name in self._names]
def __set__(self, obj, value):
raise TypeError, "object '%s' does not support assignement" % self
import types
class ObjectClass(object):
def __getattribute__(self, name):
v = object.__getattribute__(self, name)
if not isinstance(v, types.FunctionType) \
and hasattr(v, '__get__'):
return v.__get__(self, self.__class__)
return v
> instance = ObjectClass()
instance.attribute = 'First PK Elem'
instance.another_attribute = 'Second PK Elem'
instance.identifier = CompoundAttribute('attribute', 'another_attribute')
NB : Sorry, not tested.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list