Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
29766b2d
Commit
29766b2d
authored
Oct 06, 1994
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add simpler __getattr__ example and document __call__
parent
9fd48ab2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
4 deletions
+78
-4
Doc/tut.tex
Doc/tut.tex
+39
-2
Doc/tut/tut.tex
Doc/tut/tut.tex
+39
-2
No files found.
Doc/tut.tex
View file @
29766b2d
...
...
@@ -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}
Doc/tut/tut.tex
View file @
29766b2d
...
...
@@ -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}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment