Setting an attribute without calling __setattr__()

John Machin sjmachin at lexicon.net
Fri Apr 25 17:22:21 EDT 2008


On Apr 26, 7:01 am, Joshua Kugler <jkug... at bigfoot.com> wrote:
> 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__?
>

Consider reading the *second* paragraph about __setattr__ in section
3.4.2 of the Python Reference Manual.



More information about the Python-list mailing list