descriptor object for an attribute?

E

Eric Mahurin

Is there a standard way to get a descriptor object for an arbitrary
object attribute - independent of whether it uses the descriptor/
property protocol or not. I want some kind of handle/reference/
pointer to an attribute. I know I could make my own class to do this
(using the __dict__ of the object and the attribute name), but I would
like to use something standard (at least a protocol) if it exists.
All that is needed in the protocol is getter and setter methods (like
__get__ and __set__ in a descriptor). Of course it would be nice to
have a better syntax than those method names of the descriptor (I've
seen unary + for the getter and += for the setter somewhere on the
web).

Now the question I'm going to get is: why? I've used ruby (everything
is also already a reference to an object) for several years and been
through this discussion. I realize that 90% of the time you want some
kind of reference/pointer, there is a better way. But there are those
occassions where the most elegant solution is with the concept of a
reference. An obvious example in python today is a descriptor object
- it looks just like a reference (but is not a pointer) - having
getter and setter methods.

The times in C/C++ where the elegant solution is with a pointer to a
pointer is also another example. That is the situation I'm dealing
with now. I'm using a singly linked list (as opposed to a normal list/
array for performance/memory reasons). If you want to insert/delete a
node at a certain point in the list, the best thing to have access to
would be "link" (attribute or even local variable) where you want to
insert/delete. Without a reference, you'd end up with a less than
ideal solution: a) do the operation based on the previous node and
special case the situation where you want to operate at the head (no
previous node), b) do the operation based on the previous node and add
a dummy head node, or c) make the list doubly linked, do the operation
based on the current node, and either special case the head or add a
dummy head node. Really I'm dealing with a directed graph structure,
but part of it uses singly linked lists (the children of a parent).
Having a handle on any of the links in the graph would be a useful
thing.
 
L

Larry Bates

Eric said:
Is there a standard way to get a descriptor object for an arbitrary
object attribute - independent of whether it uses the descriptor/
property protocol or not. I want some kind of handle/reference/
pointer to an attribute. I know I could make my own class to do this
(using the __dict__ of the object and the attribute name), but I would
like to use something standard (at least a protocol) if it exists.
All that is needed in the protocol is getter and setter methods (like
__get__ and __set__ in a descriptor). Of course it would be nice to
have a better syntax than those method names of the descriptor (I've
seen unary + for the getter and += for the setter somewhere on the
web).

Now the question I'm going to get is: why? I've used ruby (everything
is also already a reference to an object) for several years and been
through this discussion. I realize that 90% of the time you want some
kind of reference/pointer, there is a better way. But there are those
occassions where the most elegant solution is with the concept of a
reference. An obvious example in python today is a descriptor object
- it looks just like a reference (but is not a pointer) - having
getter and setter methods.

The times in C/C++ where the elegant solution is with a pointer to a
pointer is also another example. That is the situation I'm dealing
with now. I'm using a singly linked list (as opposed to a normal list/
array for performance/memory reasons). If you want to insert/delete a
node at a certain point in the list, the best thing to have access to
would be "link" (attribute or even local variable) where you want to
insert/delete. Without a reference, you'd end up with a less than
ideal solution: a) do the operation based on the previous node and
special case the situation where you want to operate at the head (no
previous node), b) do the operation based on the previous node and add
a dummy head node, or c) make the list doubly linked, do the operation
based on the current node, and either special case the head or add a
dummy head node. Really I'm dealing with a directed graph structure,
but part of it uses singly linked lists (the children of a parent).
Having a handle on any of the links in the graph would be a useful
thing.
I believe you are looking for setattr(obj, attr, value) and
getattr(obj, attr) functions. Since everything in python its a
pointer to an object, I wasn't able to understand EXACTLY what you
were asking (an example would help).

-Larry
 
B

Bruno Desthuilliers

Eric Mahurin a écrit :
Is there a standard way to get a descriptor object for an arbitrary
object attribute - independent of whether it uses the descriptor/
property protocol or not. I want some kind of handle/reference/
pointer to an attribute.

I'm not sure to understand what you want exactly. A concrete example
would probably help. But from what I understood, you could just use a
thin wrapper like:

_marker = object()
class Accessor(object):
def __init__(self, obj, name):
self._obj = obj
self._name = name
def __call__(self, new_val=_marker):
if new_val is _marker:
return getattr(self._obj, self._name)
else:
setattr(self._obj, self._name, new_val)


class Foo(object):
def __init__(self, bar, baak=42):
self.bar = bar
self.baak = baak

foo = Foo('allo')
bar = Accessor(foo, 'bar')

assert bar() == foo.bar == 'allo'
bar(42)
assert bar() == foo.bar == 42

If that doesn't answer your question, please pardon my stupidity and
provide some more concrete example.
 
E

Eric Mahurin

Eric Mahurin a écrit :


I'm not sure to understand what you want exactly. A concrete example
would probably help. But from what I understood, you could just use a
thin wrapper like:

_marker = object()
class Accessor(object):
def __init__(self, obj, name):
self._obj = obj
self._name = name
def __call__(self, new_val=_marker):
if new_val is _marker:
return getattr(self._obj, self._name)
else:
setattr(self._obj, self._name, new_val)

class Foo(object):
def __init__(self, bar, baak=42):
self.bar = bar
self.baak = baak

foo = Foo('allo')
bar = Accessor(foo, 'bar')

assert bar() == foo.bar == 'allo'
bar(42)
assert bar() == foo.bar == 42

If that doesn't answer your question, please pardon my stupidity and
provide some more concrete example.

Thanks Bruno,

This does answer my question. The protocol you implemented looks to
match a weakref except you can also set the value by giving an
argument to __call__ (an of course it implements a "hard" ref). I was
considering this one already, but decided to use an attribute/
descriptor for getting/setting for a little better efficiency (no if
statement). I also made my reference classes forward/proxy attributes
from the underlying object. I used the attribute "_" for getting/
setting the underlying object to reduce the chances of colliding with
attributes of the underlying object.

I still don't like the resulting syntax, but it is workable. I would
have to be able to do one of these:

my_ref() # get underlying object - possible with __call__
my_ref() = obj # set underlying object - can't: no calling assign

my_ref[] # get underlying object - can't: __getitem__
requires a key/index
my_ref[] = obj # set underlying object - can't: __setitem__ requires a
key/index

Below is a stripped down version of what I'm using now. I'm showing
the references (sub-classed to links) being used in a singly linked
list (I'm really using them with a directed graph where children are
implemented with a singly linked list). When implementing these types
of structures, the concept of a "reference" (to an object reference)
can be quite handy. With a list/dict, you already have a handle to
values in a list/dict - an index/key. This isn't quite a reference,
but usually it is good enough. For other structures, a reference
becomes much more useful since you don't have this.

class Ref(object) :
'''
Anonymous reference to an object.
'''
def __init__(self, object=None) :
''' 2
'''
self._ = object

def __getattr__(self, name) :
''' 1
'''
return getattr(self._, name)


class LookupRef(Ref) :
'''
Reference to a value in a lookup (dict, list, tuple, etc).
'''
def __init__(self, lookup, key) :
self._lookup = lookup
self._key = key

_ = property(
lambda self: self._lookup.__getitem__(self._key),
lambda self, value: self._lookup.__setitem__(self._key,
value),
doc=''' 1
''')


class AttrRef(Ref) :
'''
Reference to an object attribute.
'''
def __init__(self,obj,attr) :
self._obj = obj
self._attr = attr

_ = property(
lambda self: getattr(self._obj, self._attr),
lambda self, value: setattr(self._obj, self._attr, value),
doc=''' 'abc'
''')


class Node(object) :
'''
Node (including head) in a singly linked-list.
'''
def __init__(self, data=None, next=None) :
self.data = data
self.next = next

def __add__(self, node) :
''' 'a'
'''
node.next = self
return node

def __iter__(self) :
'''
An iterator through self and all other nodes linked.
>>> [node.data for node in Node(1) + Node(2) + Node(3)]
[3, 2, 1]
'''
node = self
while node :
yield node
node = node.next

def __repr__(self) :
'''
Gives the coordinates and the children.
Node(1) + Node(2)
'''
return ' + '.join(reversed(
['Node('+repr(node.data)+')' for node in self]
))

def push_next(self, node) :
'''
'''
node.next = self.next
self.next = node

def pop_next(self) :
''' Node(1) + Node(3)
'''
node = self.next
self.next = node.next
node.next = None
return node


class Link(Ref) :
'''
A link (type of reference) to a Node.
'''
def pop(self) :
'''
Pop off the current linked node (returning it) and link to the
next node.
Node(1) + Node(2)
'''
node = self._
self._ = node.next
node.next = None
return node

def push(self, node) :
'''
Link to a new node and make the previous linked node the next
one.
Node(1) + Node(2) + Node(3)
'''
node.next = self._
self._ = node

def __iter__(self) :
'''
Iterator to self and next links.
... # remove first node with even data
... if not link.data%2 :
... node = link.pop()
... break Node(4)
'''
ref = self
while ref._ :
yield ref
ref = AttrLink(ref._,'next')

class AttrLink(AttrRef, Link) : pass

class LookupLink(LookupRef, Link) : pass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top