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
ca3f6c8c
Kaydet (Commit)
ca3f6c8c
authored
Eki 06, 1994
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Removed description of class.init() method.
Added news about new operator overloading and __getattr__ etc.
üst
16cd7f9f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
194 additions
and
44 deletions
+194
-44
tut.tex
Doc/tut.tex
+97
-22
tut.tex
Doc/tut/tut.tex
+97
-22
No files found.
Doc/tut.tex
Dosyayı görüntüle @
ca3f6c8c
...
...
@@ -2438,28 +2438,8 @@ Methods may call other methods by using method attributes of the
The instantiation operation (``calling'' a class object) creates an
empty object. Many classes like to create objects in a known initial
state. In early versions of Python, there was no special syntax to
enforce this (see below), but a convention was widely used:
add a method named
\verb
\
init
\
to the class,
which initializes the instance (by assigning to some important data
attributes) and returns the instance itself. For example, class
\verb
\
Bag
\
above could have the following method:
\begin{verbatim}
def init(self):
self.empty()
return self
\end{verbatim}
The client can then create and initialize an instance in one
statement, as follows:
\begin{verbatim}
x = Bag().init()
\end{verbatim}
In later versions of Python, a special method named
\verb
\
__init__
\
may be
defined instead:
state. Therefore a class may define a special method named
\verb
\
__init__
\
, like this:
\begin{verbatim}
def
__
init
__
(self):
...
...
@@ -3052,4 +3032,99 @@ raise an exception. For example:
f.close()
\end{verbatim}
\section
{
New Class Features in Release 1.1
}
Two 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.
\subsection
{
New Operator Overloading
}
It is no longer necessary to coerce both sides of an operator to the
same class or type. A class may still provide a
\code
{__
coerce
__}
method, but this method may return objects of different types or
classes if it feels like it. If no
\code
{__
coerce
__}
is defined, any
argument type or class is acceptable.
In order to make it possible to implement binary operators where the
right-hand side is a class instance but the left-hand side is not,
without using coercions, right-hand versions of all binary operators
may be defined. These have an `r' prepended to their name,
e.g.
\code
{__
radd
__}
.
For example, here's a very simple class for representing times. Times
are initialized from a number of seconds (like time.time()). Times
are printed like this:
\code
{
Thu Oct 6 14:20:06 1994
}
. Subtracting
two Times gives their difference in seconds. Adding or subtracting a
Time and a number gives a new Time. You can't add two times, nor can
you subtract a Time from a number.
\begin{verbatim}
import time
class Time:
def
__
init
__
(self, seconds):
self.seconds = seconds
def
__
repr
__
(self):
return time.ctime(self.seconds)
def
__
add
__
(self, x):
return Time(self.seconds + x)
__
radd
__
=
__
add
__
# support for x+t
def
__
sub
__
(self, x):
if hasattr(x, 'seconds'): # test if x could be a Time
return self.seconds - x.seconds
else:
return self.seconds - x
now = Time(time.time())
tomorrow = 24*3600 + now
yesterday = now - today
print tomorrow - yesterday # prints 172800
\end{verbatim}
\subsection
{
Trapping Attribute Access
}
You can define three new ``magic'' methods in a class now:
\code
{__
getattr
__
(self, name)
}
,
\code
{__
setattr
__
(self, name, value)
}
and
\code
{__
delattr
__
(self, name)
}
.
The
\code
{__
getattr
__}
method is called when an attribute access fails,
i.e. when an attribute access would otherwise raise AttributeError --
this is
{
\em
after
}
the instance's dictionary and its class hierarchy
have been searched for the named attribute. Note that if this method
attempts to access any undefined instance attribute it will be called
recursively!
The
\code
{__
setattr
__}
and
\code
{__
delattr
__}
methods are called when
assignment to, respectively deletion of an attribute are attempted.
They are called
{
\em
instead
}
of the normal action (which is to insert
or delete the attribute in the instance dictionary). If either of
these methods most set or delete any attribute, they can only do so by
using the instance dictionary directly --
\code
{
self.
__
dict
__}
-- else
they would be called recursively.
For example, here's a near-universal ``Wrapper'' class that passes all
its attribute accesses to another object. Note how the
\code
{__
init
__}
method inserts the wrapped object in
\code
{
self.
__
dict
__}
in order to avoid endless recursion
(
\code
{__
setattr
__}
would call
\code
{__
getattr
__}
which would call
itself recursively).
\begin{verbatim}
class Wrapper:
def
__
init
__
(self, wrapped):
self.
__
dict
__
['wrapped'] = wrapped
def
__
getattr
__
(self, name):
return getattr(self.wrapped, name)
def
__
setattr
__
(self, name, value):
setattr(self.wrapped, value)
def
__
delattr
__
(self, name):
delattr(self.wrapped, name)
import sys
f = Wrapper(sys.stdout)
f.write('hello world
\n
') # prints 'hello world'
\end{verbatim}
\end{document}
Doc/tut/tut.tex
Dosyayı görüntüle @
ca3f6c8c
...
...
@@ -2438,28 +2438,8 @@ Methods may call other methods by using method attributes of the
The instantiation operation (``calling'' a class object) creates an
empty object. Many classes like to create objects in a known initial
state. In early versions of Python, there was no special syntax to
enforce this (see below), but a convention was widely used:
add a method named
\verb
\
init
\
to the class,
which initializes the instance (by assigning to some important data
attributes) and returns the instance itself. For example, class
\verb
\
Bag
\
above could have the following method:
\begin{verbatim}
def init(self):
self.empty()
return self
\end{verbatim}
The client can then create and initialize an instance in one
statement, as follows:
\begin{verbatim}
x = Bag().init()
\end{verbatim}
In later versions of Python, a special method named
\verb
\
__init__
\
may be
defined instead:
state. Therefore a class may define a special method named
\verb
\
__init__
\
, like this:
\begin{verbatim}
def
__
init
__
(self):
...
...
@@ -3052,4 +3032,99 @@ raise an exception. For example:
f.close()
\end{verbatim}
\section
{
New Class Features in Release 1.1
}
Two 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.
\subsection
{
New Operator Overloading
}
It is no longer necessary to coerce both sides of an operator to the
same class or type. A class may still provide a
\code
{__
coerce
__}
method, but this method may return objects of different types or
classes if it feels like it. If no
\code
{__
coerce
__}
is defined, any
argument type or class is acceptable.
In order to make it possible to implement binary operators where the
right-hand side is a class instance but the left-hand side is not,
without using coercions, right-hand versions of all binary operators
may be defined. These have an `r' prepended to their name,
e.g.
\code
{__
radd
__}
.
For example, here's a very simple class for representing times. Times
are initialized from a number of seconds (like time.time()). Times
are printed like this:
\code
{
Thu Oct 6 14:20:06 1994
}
. Subtracting
two Times gives their difference in seconds. Adding or subtracting a
Time and a number gives a new Time. You can't add two times, nor can
you subtract a Time from a number.
\begin{verbatim}
import time
class Time:
def
__
init
__
(self, seconds):
self.seconds = seconds
def
__
repr
__
(self):
return time.ctime(self.seconds)
def
__
add
__
(self, x):
return Time(self.seconds + x)
__
radd
__
=
__
add
__
# support for x+t
def
__
sub
__
(self, x):
if hasattr(x, 'seconds'): # test if x could be a Time
return self.seconds - x.seconds
else:
return self.seconds - x
now = Time(time.time())
tomorrow = 24*3600 + now
yesterday = now - today
print tomorrow - yesterday # prints 172800
\end{verbatim}
\subsection
{
Trapping Attribute Access
}
You can define three new ``magic'' methods in a class now:
\code
{__
getattr
__
(self, name)
}
,
\code
{__
setattr
__
(self, name, value)
}
and
\code
{__
delattr
__
(self, name)
}
.
The
\code
{__
getattr
__}
method is called when an attribute access fails,
i.e. when an attribute access would otherwise raise AttributeError --
this is
{
\em
after
}
the instance's dictionary and its class hierarchy
have been searched for the named attribute. Note that if this method
attempts to access any undefined instance attribute it will be called
recursively!
The
\code
{__
setattr
__}
and
\code
{__
delattr
__}
methods are called when
assignment to, respectively deletion of an attribute are attempted.
They are called
{
\em
instead
}
of the normal action (which is to insert
or delete the attribute in the instance dictionary). If either of
these methods most set or delete any attribute, they can only do so by
using the instance dictionary directly --
\code
{
self.
__
dict
__}
-- else
they would be called recursively.
For example, here's a near-universal ``Wrapper'' class that passes all
its attribute accesses to another object. Note how the
\code
{__
init
__}
method inserts the wrapped object in
\code
{
self.
__
dict
__}
in order to avoid endless recursion
(
\code
{__
setattr
__}
would call
\code
{__
getattr
__}
which would call
itself recursively).
\begin{verbatim}
class Wrapper:
def
__
init
__
(self, wrapped):
self.
__
dict
__
['wrapped'] = wrapped
def
__
getattr
__
(self, name):
return getattr(self.wrapped, name)
def
__
setattr
__
(self, name, value):
setattr(self.wrapped, value)
def
__
delattr
__
(self, name):
delattr(self.wrapped, name)
import sys
f = Wrapper(sys.stdout)
f.write('hello world
\n
') # prints 'hello world'
\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