473,763 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2030
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._o bj, self._name)
else:
setattr(self._o bj, 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.quelque ch...@free.quel quepart.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._o bj, self._name)
else:
setattr(self._o bj, 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__(sel f, 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(local s(),'v')
r._ = 1
v
1
''')
class AttrRef(Ref) :
'''
Reference to an object attribute.
'''
def __init__(self,o bj,attr) :
self._obj = obj
self._attr = attr

_ = property(
lambda self: getattr(self._o bj, self._attr),
lambda self, value: setattr(self._o bj, 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(no de.data)+')' for node in self]
))

def push_next(self, node) :
'''
>>l = Node(1)+Node(2)
l.push_next(N ode(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)+No de(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)+No de(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)+No de(2)+Node(3)+N ode(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(AttrRe f, Link) : pass

class LookupLink(Look upRef, Link) : pass

Apr 14 '07 #4

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

Similar topics

9
1420
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 __get__(self, obj, type=None): print "in get method"
0
1101
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 __get__(None, a) called on it)? Pre-metaclasses, one might assume it was isinstance(a, (types.TypeType, types.ClassType))
0
1096
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 object they are wrapping, whereas the descriptors written in C do not. For example, taking the code from http://users.rcn.com/python/download/Descriptor.htm which implements what one would think is a straightforward implementation: class...
4
1244
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
9758
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
1772
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
1095
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 subclass to be able to read and write to the _same_ slot/variable. But I can't figure out how to do it. My attempts so far have led me to using a descriptor as follow, but it doesn't get me where I want to be: class sharedClassVar(object):
9
1439
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. Any trick in the book (metaclasses, descriptors, etc) is fair game. Raymond -------- how we currently localize method access with name mangling
3
1575
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 help but help(Rectangle) shows only two lines : " Help on class Rectangle in module basis2:
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.