Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
29766b2d
Kaydet (Commit)
29766b2d
authored
Eki 06, 1994
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add simpler __getattr__ example and document __call__
üst
9fd48ab2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
4 deletions
+78
-4
tut.tex
Doc/tut.tex
+39
-2
tut.tex
Doc/tut/tut.tex
+39
-2
No files found.
Doc/tut.tex
Dosyayı görüntüle @
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
Dosyayı görüntüle @
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