Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Modifying an instances dict to change attribute lookup

Reply
Thread Tools

Modifying an instances dict to change attribute lookup

 
 
jslowery@gmail.com
Guest
Posts: n/a
 
      02-20-2006
Hmm, I know this is something fundamental about how Python implements
predicate dispatch, but for some reason I believed that this would
work:

class delegate_dict(dict):
def __init__(self, orig, deleg):
dict.__init__(self, orig)
self.deleg = deleg

def __getitem__(self, name):
print 'here:', name
try:
v = dict.__getitem__(self, name)
except KeyError:
return self.deleg.__getitem__(name)
return v

def delegate(target, source):
target.__dict__ = delegate_dict(target.__dict__, source.__dict__)

As a simple usage, give:


class A(object):
pass

class B(object):
def __init__(self, o):
delegate(self, o)

a = A()
a.test = 1
b = B(a)
print b.__dict__.__getitem__('test')

# This goes down in flames
print b.test


And then a dumb example showing the concept I assumed was a given:

class C(object):
pass

c = C()
c.x = 1
assert c.__dict__.__getitem__('x') == c.x

Could someone please tell me why the first example using a customized
dict does not perform as advertised?

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      02-20-2006
<> wrote:
...
> c = C()
> c.x = 1
> assert c.__dict__.__getitem__('x') == c.x
>
> Could someone please tell me why the first example using a customized
> dict does not perform as advertised?


Because Python's __getattribute__ is currently not EXACTLY as you
imagine it to be, but slightly different, say something like:
dict.__getitem__(c, 'x')
at the crucial step rather than c.__getitem__. More generally, when (on
any type or newstyle class -- oldstyle legacy classes are different) a
special method gets implicitly invoked, it's invoked on the type, not on
the instance (no per-instance overriding in this case); and, for speed,
many internal dict lookups do "dict.__getitem__(obj,name)" rather than
"type(obj).__getitem__(obj, name)".

The documented way to do what you want in Python is by overriding
__getattr__ (which gets called when other normal lookup procedures fail
and would, absent __getattr__, raise AttributeError), not by trying to
install as the __dict__ something whose type isn't exactly __dict__.
Other approaches may work, but they depend on implementation accidents
which are liable to change with every passing breeze.


Alex
 
Reply With Quote
 
 
 
Reply

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
reverse dict lookup & Relation class Aaron Brady Python 8 01-18-2009 01:54 AM
trap setting attribute when the attribute is dict Python 6 09-04-2007 02:51 AM
dict!ident as equivalent of dict["ident"] Alexander Kozlovsky Python 5 05-22-2006 08:06 AM
Dict lookup shortcut? M. Clift Python 5 10-13-2004 09:20 AM
Re: dict->XML->dict? Or, passing small hashes through text? Skip Montanaro Python 0 08-15-2003 03:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57