468,738 Members | 2,559 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

Re: Setting an attribute without calling __setattr__()


"Joshua Kugler" <jk*****@bigfoot.comwrote in message
news:fu**********@ger.gmane.org...
| OK, I'm sure the answer is staring me right in the face--whether that
answer
| be "you can't do that" or "here's the really easy way--but I am stuck.
I'm
| writing an object to proxy both lists (subscriptable iterables, really)
and
| dicts.
|
| My init lookslike this:
|
| def __init__(self, obj=None):
| if type(obj).__name__ in 'list|tuple|set|frozenset':
| self.me = []
| for v in obj:
| self.me.append(ObjectProxy(v))
| elif type(obj) == dict:
| self.me = {}
| for k,v in obj.items():
| self.me[k] = ObjectProxy(v)
|
| and I have a __setattr__ defined like so:
|
| def __setattr__(self, name, value):
| self.me[name] = ObjectProxy(value)
|
| You can probably see the problem.
|
| While doing an init, self.me = {} or self.me = [] calls __setattr__,
which
| then ends up in an infinite loop, and even it it succeeded
|
| self.me['me'] = {}
|
| is not what I wanted in the first place.
|
| Is there a way to define self.me without it firing __setattr__?

I believe self.__dict__['me'] = {} is one standard idiom.

Jun 27 '08 #1
0 642

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.