How does "delegation" work as per the FAQ?
Rene Pijlman
reply.in.the.newsgroup at my.address.is.invalid
Thu Dec 25 12:08:24 EST 2003
Section 6.5 "What is delegation?" of the FAQ says:
"Python programmers can easily implement delegation. For example, the
following class implements a class that behaves like a file but converts
all written data to uppercase:
class UpperOut:
def __init__(self, outfile):
self.__outfile = outfile
def write(self, s):
self.__outfile.write(s.upper())
def __getattr__(self, name):
return getattr(self.__outfile, name)
[...] All other methods are delegated to the underlying self.__outfile
object. The delegation is accomplished via the __getattr__ method; consult
the language reference for more information about controlling attribute
access."
http://www.python.org/doc/faq/programming.html#what-is-delegation
I don't understand how __getattr__ accomplishes delegation of other
methods. I'd expect __getattr__ to be called for attribute access, not for
method calls. And how would the arguments of a method call be passed on?
The link to the language reference doesn't help much. It says about
__getattr__: "Called when an attribute lookup has not found the attribute
in the usual places". There is no mention of method calls.
--
René Pijlman
More information about the Python-list
mailing list