Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python Descriptor as Instance Attribute

Thread Tools

Python Descriptor as Instance Attribute

Hua Yanghao
Posts: n/a
Hi all,
Currently descriptors only work as class attribute,
and doesn't work as a descriptor when it is an instance attribute.

e.g. if we have descriptor class DescriptorTest,
class Dummy(object):
d = DescriptorTest()

class Dummy2(object):
def __init__(self):
self.d = DescriptorTest()

The instance of Dummy2 does not invoke the descriptor protocols on "d".
Whereas Dummy() instances all share the same descriptor "d".

Yes I know d.__get__() have an "instance" parameter which can be used
to store per-instance values, but sometimes it is just not enough (or it is that
I do not know a better approach exists).

Suppose the below scenario, I want to model a "Register". A register is consist
of some fields, which have different number of bits.
e.g. a 32 bit register divided into 3 field, bit[0] to bit[7] is
called M, bit[8] to bit[15]
is called N, and bit[16] to bit[31] is called Z.
I want to model a register that, when instantiated as "reg", reg.M/N/Z
can directly
reference each field, calling a descriptor protocol to verify and
return the values.

Yes, I know I can use metaclass to create a different class for
different registers,
but that's not seems to be a very smart approach here, as I want all
register instances
be of type "Register". But the default python protocol will not run
descriptor protocols
if a descriptor is an instance attribute. I'm sure I should not be the
only one that facing
this issue and I googled around and found a solution to redefine the
__setattr__ and
__getattribute__ to look up the descriptor protocols first:

107 def __getattribute__(self, name):
108 value = object.__getattribute__(self, name)
109 if hasattr(value, '__get__'):
110 value = value.__get__(self, self.__class__)
111 return value
113 def __setattr__(self, name, value):
114 try:
115 obj = object.__getattribute__(self, name)
116 except AttributeError:
117 pass
118 else:
119 if hasattr(obj, '__set__'):
120 return obj.__set__(self, value)
121 return object.__setattr__(self, name, value)

This works like a charm and each instance of "Register" now invoke the
descriptors properly.

I just do not understand, why such behavior is not a default in python.
Or, is there a better design pattern here?

Thanks & Best Regards,
Hua Yanghao
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
class object's attribute is also the instance's attribute? 陈伟 Python 9 08-30-2012 03:20 PM
Re: get descriptor from instance Gabriel Genellina Python 6 02-19-2009 06:42 PM
descriptor object for an attribute? Eric Mahurin Python 3 04-14-2007 10:34 PM
Implied instance attribute creation when referencing a class attribute Russell Warren Python 5 01-17-2006 05:07 AM
class attribute to instance attribute Donnal Walter Python 4 07-06-2005 04:35 AM