diff options
author | Guido van Rossum <guido@python.org> | 1994-10-06 15:33:25 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1994-10-06 15:33:25 (GMT) |
commit | 29766b2da6303bab42d491913c0e57c77803699d (patch) | |
tree | 3b8b0789a4fa88b478b1bf7e84eef4ced0aba452 /Doc | |
parent | 9fd48ab27e8e012e45319d5b08357727e9672183 (diff) | |
download | cpython-29766b2da6303bab42d491913c0e57c77803699d.zip cpython-29766b2da6303bab42d491913c0e57c77803699d.tar.gz cpython-29766b2da6303bab42d491913c0e57c77803699d.tar.bz2 |
Add simpler __getattr__ example and document __call__
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/tut.tex | 41 | ||||
-rw-r--r-- | Doc/tut/tut.tex | 41 |
2 files changed, 78 insertions, 4 deletions
diff --git a/Doc/tut.tex b/Doc/tut.tex index 340be69..6a1bcd9 100644 --- a/Doc/tut.tex +++ b/Doc/tut.tex @@ -3035,9 +3035,10 @@ raise an exception. For example: \section{New Class Features in Release 1.1} -Two changes have been made to classes: the operator overloading +Semoe changes have been made to classes: the operator overloading mechanism is more flexible, providing more support for non-numeric use -of operators, and it is possible to trap attribute accesses. +of operators (including calling an object as if it were a function), +and it is possible to trap attribute accesses. \subsection{New Operator Overloading} @@ -3127,4 +3128,40 @@ f = Wrapper(sys.stdout) f.write('hello world\n') # prints 'hello world' \end{verbatim} +A simpler example of \code{__getattr__} is an attribute that is +computed each time (or the first time) it it accessed. For instance: + +\begin{verbatim} +from math import pi + +class Circle: + def __init__(self, radius): + self.radius = radius + def __getattr__(self, name): + if name == 'circumference': + return 2 * pi * self.radius + if name == 'diameter': + return 2 * self.radius + if name == 'area': + return pi * pow(self.radius, 2) + raise AttributeError, name +\end{verbatim} + +\subsection{Calling a Class Instance} + +If a class defines a method \code{__call__} it is possible to call its +instances as if they were functions. For example: + +\begin{verbatim} +class PresetSomeArguments: + def __init__(self, func, *args): + self.func, self.args = func, args + def __call__(self, *args): + return apply(self.func, self.args + args) + +f = PresetSomeArguments(pow, 2) # f(i) computes powers of 2 +for i in range(10): print f(i), # prints 1 2 4 8 16 32 64 128 256 512 +print # append newline +\end{verbatim} + \end{document} diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex index 340be69..6a1bcd9 100644 --- a/Doc/tut/tut.tex +++ b/Doc/tut/tut.tex @@ -3035,9 +3035,10 @@ raise an exception. For example: \section{New Class Features in Release 1.1} -Two changes have been made to classes: the operator overloading +Semoe changes have been made to classes: the operator overloading mechanism is more flexible, providing more support for non-numeric use -of operators, and it is possible to trap attribute accesses. +of operators (including calling an object as if it were a function), +and it is possible to trap attribute accesses. \subsection{New Operator Overloading} @@ -3127,4 +3128,40 @@ f = Wrapper(sys.stdout) f.write('hello world\n') # prints 'hello world' \end{verbatim} +A simpler example of \code{__getattr__} is an attribute that is +computed each time (or the first time) it it accessed. For instance: + +\begin{verbatim} +from math import pi + +class Circle: + def __init__(self, radius): + self.radius = radius + def __getattr__(self, name): + if name == 'circumference': + return 2 * pi * self.radius + if name == 'diameter': + return 2 * self.radius + if name == 'area': + return pi * pow(self.radius, 2) + raise AttributeError, name +\end{verbatim} + +\subsection{Calling a Class Instance} + +If a class defines a method \code{__call__} it is possible to call its +instances as if they were functions. For example: + +\begin{verbatim} +class PresetSomeArguments: + def __init__(self, func, *args): + self.func, self.args = func, args + def __call__(self, *args): + return apply(self.func, self.args + args) + +f = PresetSomeArguments(pow, 2) # f(i) computes powers of 2 +for i in range(10): print f(i), # prints 1 2 4 8 16 32 64 128 256 512 +print # append newline +\end{verbatim} + \end{document} |