Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29766b2

Browse files
committed
Add simpler __getattr__ example and document __call__
1 parent 9fd48ab commit 29766b2

2 files changed

Lines changed: 78 additions & 4 deletions

File tree

Doc/tut.tex

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,9 +3035,10 @@ \section{Else Clause For Try Statement}
30353035

30363036
\section{New Class Features in Release 1.1}
30373037

3038-
Two changes have been made to classes: the operator overloading
3038+
Semoe changes have been made to classes: the operator overloading
30393039
mechanism is more flexible, providing more support for non-numeric use
3040-
of operators, and it is possible to trap attribute accesses.
3040+
of operators (including calling an object as if it were a function),
3041+
and it is possible to trap attribute accesses.
30413042

30423043
\subsection{New Operator Overloading}
30433044

@@ -3127,4 +3128,40 @@ \subsection{Trapping Attribute Access}
31273128
f.write('hello world\n') # prints 'hello world'
31283129
\end{verbatim}
31293130

3131+
A simpler example of \code{__getattr__} is an attribute that is
3132+
computed each time (or the first time) it it accessed. For instance:
3133+
3134+
\begin{verbatim}
3135+
from math import pi
3136+
3137+
class Circle:
3138+
def __init__(self, radius):
3139+
self.radius = radius
3140+
def __getattr__(self, name):
3141+
if name == 'circumference':
3142+
return 2 * pi * self.radius
3143+
if name == 'diameter':
3144+
return 2 * self.radius
3145+
if name == 'area':
3146+
return pi * pow(self.radius, 2)
3147+
raise AttributeError, name
3148+
\end{verbatim}
3149+
3150+
\subsection{Calling a Class Instance}
3151+
3152+
If a class defines a method \code{__call__} it is possible to call its
3153+
instances as if they were functions. For example:
3154+
3155+
\begin{verbatim}
3156+
class PresetSomeArguments:
3157+
def __init__(self, func, *args):
3158+
self.func, self.args = func, args
3159+
def __call__(self, *args):
3160+
return apply(self.func, self.args + args)
3161+
3162+
f = PresetSomeArguments(pow, 2) # f(i) computes powers of 2
3163+
for i in range(10): print f(i), # prints 1 2 4 8 16 32 64 128 256 512
3164+
print # append newline
3165+
\end{verbatim}
3166+
31303167
\end{document}

Doc/tut/tut.tex

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,9 +3035,10 @@ \section{Else Clause For Try Statement}
30353035

30363036
\section{New Class Features in Release 1.1}
30373037

3038-
Two changes have been made to classes: the operator overloading
3038+
Semoe changes have been made to classes: the operator overloading
30393039
mechanism is more flexible, providing more support for non-numeric use
3040-
of operators, and it is possible to trap attribute accesses.
3040+
of operators (including calling an object as if it were a function),
3041+
and it is possible to trap attribute accesses.
30413042

30423043
\subsection{New Operator Overloading}
30433044

@@ -3127,4 +3128,40 @@ \subsection{Trapping Attribute Access}
31273128
f.write('hello world\n') # prints 'hello world'
31283129
\end{verbatim}
31293130

3131+
A simpler example of \code{__getattr__} is an attribute that is
3132+
computed each time (or the first time) it it accessed. For instance:
3133+
3134+
\begin{verbatim}
3135+
from math import pi
3136+
3137+
class Circle:
3138+
def __init__(self, radius):
3139+
self.radius = radius
3140+
def __getattr__(self, name):
3141+
if name == 'circumference':
3142+
return 2 * pi * self.radius
3143+
if name == 'diameter':
3144+
return 2 * self.radius
3145+
if name == 'area':
3146+
return pi * pow(self.radius, 2)
3147+
raise AttributeError, name
3148+
\end{verbatim}
3149+
3150+
\subsection{Calling a Class Instance}
3151+
3152+
If a class defines a method \code{__call__} it is possible to call its
3153+
instances as if they were functions. For example:
3154+
3155+
\begin{verbatim}
3156+
class PresetSomeArguments:
3157+
def __init__(self, func, *args):
3158+
self.func, self.args = func, args
3159+
def __call__(self, *args):
3160+
return apply(self.func, self.args + args)
3161+
3162+
f = PresetSomeArguments(pow, 2) # f(i) computes powers of 2
3163+
for i in range(10): print f(i), # prints 1 2 4 8 16 32 64 128 256 512
3164+
print # append newline
3165+
\end{verbatim}
3166+
31303167
\end{document}

0 commit comments

Comments
 (0)