Setting an attribute without calling __setattr__()

Joshua Kugler jkugler at bigfoot.com
Fri Apr 25 17:01:45 EDT 2008


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

If not, it's not a huge deal, as having this class read-only for now won't
be a problem, but I was just trying to make it read/write.

Thanks!

j




More information about the Python-list mailing list