473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

descriptor object for an attribute?

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.

Apr 11 '07 #1
3 2010
Eric Mahurin wrote:
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
Apr 11 '07 #2
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.
Apr 11 '07 #3
On Apr 11, 3:07 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
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.
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) :
'''
>>r = Ref(-1)
r._
-1
>>r._ = 2
r._
2
'''
self._ = object

def __getattr__(self, name) :
'''
>>r = Ref(-1)
r.__abs__()
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='''
>>r = LookupRef(('a','b','c'),1)
r._
'b'
>>v = 0
r = LookupRef(locals(),'v')
r._ = 1
v
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='''
>>class Foo : bar = 123
f = Foo()
r = AttrRef(f,'bar')
r._
123
>>r._ = 'abc'
f.bar
'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) :
'''
>>t = Node('a')+Node('b')
t.data
'b'
>>t.next.data
'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)
Node(1) + Node(2)
'''
return ' + '.join(reversed(
['Node('+repr(node.data)+')' for node in self]
))

def push_next(self, node) :
'''
>>l = Node(1)+Node(2)
l.push_next(Node(3))
'''
node.next = self.next
self.next = node

def pop_next(self) :
'''
>>l = Node(1)+Node(2)+Node(3)
l.pop_next()
Node(2)
>>l
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.
>>l = Link(Node(1)+Node(2)+Node(3))
l.pop()
Node(3)
>>l._
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.
>>l = Link(Node(1)+Node(2))
l.push(Node(3))
l._
Node(1) + Node(2) + Node(3)
'''
node.next = self._
self._ = node

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

class AttrLink(AttrRef, Link) : pass

class LookupLink(LookupRef, Link) : pass

Apr 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Roth | last post by:
Using Python 2.2.3, I create this script: class AnObject(object): "This might be a descriptor, but so far it doesn't seem like it" def __init__(self, somedata): self.it = somedata def...
0
by: John Perks and Sarah Mount | last post by:
I'm talk from the point of view of descriptors. Consider a.x = lambda self:None # simple function When a.x is later got, what criterion is used to see if a class (and so the func would have...
0
by: Eric Huss | last post by:
I'm trying to write a descriptor (similar to the EiffelDescriptor example in the Demo directory). However, I noticed an odd behavior that descriptors written in pure Python mask the underlying...
4
by: john | last post by:
Hello, I was wondering if someone could explain the following situation to me please: >>> class C(object): def f(self): pass >>> c = C() >>> c.f
8
by: Joe | last post by:
Hi, I want to use the C library function mkstemp. It returns a unix file descriptor (int) to an open file. How do I convert the file descriptor to a valid ofstream object? Thanks, Joe
6
by: Siddharth Taneja | last post by:
Hi, I have a very simple prg over here, trying to read the lines of a file #include <iostream> #include <fstream> #include <iostream> #include <string> using namespace std;
3
by: Gary Stephenson | last post by:
I want to define a class-level attribute that will be shared by all subclasses. That is, I want this class, every subclass of this class, every instance of this class and every instance of every...
9
by: Raymond Hettinger | last post by:
I had an idea but no time to think it through. Perhaps the under-under name mangling trick can be replaced (in Py3.0) with a suitably designed decorator. Your challenge is to write the decorator....
3
by: cyril giraudon | last post by:
Hello, I try to use python descriptors to define attributes with default value (the code is reported below). But apparently, it breaks the docstring mechanism. help(Basis) shows the right...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.