Python Macros

G. S. Hayes sjdevnull at
Tue Oct 5 17:38:29 CEST 2004

Arich Chanachai <macrocosm at> wrote in message news:<mailman.4287.1096945195.5135.python-list at>...
> What if foo doesn't know about bar(), can I tell foo that perhaps it 
> should send the bar() message

Sure.  Just override __getattr__ in foo's class.  The below is a
simple example, in real life you might want a way to designate where a
particular Foo delegates unknown requests (e.g to a particular Bar

class Boo(object):
    def bar(klass, arg):
        print "bar", arg
    bar = classmethod(bar)

class Foo(object):
    def __getattr__(self, attr):
        if not hasattr(self, attr):
            return getattr(Boo, attr)
        return self.__dict__[attr]
    def foo(self, arg):
        print "foo", arg

foo = Foo()"hello")"goodbye")

More information about the Python-list mailing list