Re: Python Macros
From: G. S. Hayes (sjdevnull_at_yahoo.com)
Date: 10/05/04
- Next message: eric_bellard: "Using JYTHON inside ANT : access os module -> "ImportError: no module named javaos""
- Previous message: Chris Malcolm: "Re: AI Mind (Was: Sound capture)"
- In reply to: Arich Chanachai: "Re: Python Macros"
- Next in thread: Alex Martelli: "Re: Python Macros"
- Reply: Alex Martelli: "Re: Python Macros"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 5 Oct 2004 08:38:29 -0700
Arich Chanachai <macrocosm@fastmail.fm> wrote in message news:<mailman.4287.1096945195.5135.python-list@python.org>...
> What if foo doesn't know about bar(), can I tell foo that perhaps it
> should send the bar() message to...say....boo?
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
instance)
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()
foo.foo("hello")
foo.bar("goodbye")
- Next message: eric_bellard: "Using JYTHON inside ANT : access os module -> "ImportError: no module named javaos""
- Previous message: Chris Malcolm: "Re: AI Mind (Was: Sound capture)"
- In reply to: Arich Chanachai: "Re: Python Macros"
- Next in thread: Alex Martelli: "Re: Python Macros"
- Reply: Alex Martelli: "Re: Python Macros"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|