design question

Mark McEahern marklists at mceahern.com
Thu Sep 26 14:00:22 EDT 2002


[Gonçalo Rodrigues]
> Well, I have thought that somewhere further down the way some (though
> not all) of the wrapping code could be factored out in some kind of
> metaclass, and then I would just set __metaclass__ at module level. But
> since I am still at the entrance, I am kinda polling for ideas.
>
> Assuming that my description of the problem is enough, how would you go
> about writing a metaclass as a general wrapper?

Hey, no fair calling my bluff!  <wink>

For starters, please tell me how come this isn't sufficient:

class Wrapper(object):

    def __init__(self, wrapped_class):
        self._wrapped_class = wrapped_class()

    def __getattribute__(self, name):
        if __debug__:
            print "calling __getattribute__(%s)" % name
        wrapped_class_attr_name = "_wrapped_class"
        wrapped_class = object.__getattribute__(self,
wrapped_class_attr_name)
        if name == wrapped_class_attr_name:
            return wrapped_class
        else:
            return getattr(wrapped_class, name)

    def __setattr__(self, name, value):
        if __debug__:
            print "calling __setattr__(%s, %s)" % (name, value)
        wrapped_class_attr_name = "_wrapped_class"
        if name == wrapped_class_attr_name:
            object.__setattr__(self, name, value)
        else:
            setattr(self._wrapped_class, name, value)

// m





More information about the Python-list mailing list