Setting an attribute without calling __setattr__()

Terry Reedy tjreedy at udel.edu
Fri Apr 25 18:38:38 EDT 2008


"Joshua Kugler" <jkugler at bigfoot.com> wrote in message 
news:futgrq$ih6$1 at 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. 






More information about the Python-list mailing list