Modifying an instances dict to change attribute lookup

J

jslowery

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?
 
A

Alex Martelli

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
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top