Nick Coghlan wrote:
There *is* a problem with using __getattr__ though - any attribute in the chained namespaces that is shadowed by a class attribute (like 'update') will be picked up from the class, not from the chained namespaces. So we do need to use __getattribute__ to change that lookup order. However, given the amount of grief the default lookup behaviour causes, I think the place to do that is in Namespace itself:

    class Namespace(object):
        # otherwise unchanged
        def __getattribute__(self, name):
            """Namespaces do NOT default to looking in their class dict
            for non-magic attributes
            """
            getattribute = super(Namespace, self).__getattribute__
            try:
                return getattribute("__dict__")[name]
            except KeyError:
                if name.startswith('__') and name.endswith('__'):
                    # Employ the default lookup system for magic names
                    return getattribute(name)
                else:
                    # Skip the default lookup system for normal names
                    if hasattr(self, "__getattr__"):
                        return getattribute("__getattr__")(name)
                    else:
                        raise AttributeError(name)

Hmmm... This does seem sensible. And sidesteps the issue about suggesting that subclasses use Namespace.update instead of namespaceinstance.update -- the latter just won't work! (This is a Good Thing, IMHO.)


Anyway, it is probably worth digging into the descriptor machinery a bit further in order to design a lookup scheme that is most appropriate for namespaces, but the above example has convinced me that object.__getattribute__ is NOT it :)

Yeah, I'm going to look into this a bit more too, but let me know if you have any more insights in this area.


Steve
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to