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
29c1b97d
Kaydet (Commit)
29c1b97d
authored
Eki 09, 1994
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
__call__, __getattr__ c.s.
üst
2e61103c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
102 additions
and
4 deletions
+102
-4
ref3.tex
Doc/ref/ref3.tex
+51
-2
ref3.tex
Doc/ref3.tex
+51
-2
No files found.
Doc/ref/ref3.tex
Dosyayı görüntüle @
29c1b97d
...
@@ -613,10 +613,14 @@ must explicitly call it to ensure proper deletion of the base class
...
@@ -613,10 +613,14 @@ must explicitly call it to ensure proper deletion of the base class
part of the instance. Note that it is possible for the
\code
{__
del
__}
part of the instance. Note that it is possible for the
\code
{__
del
__}
method to postpone destruction of the instance by creating a new
method to postpone destruction of the instance by creating a new
reference to it. It may then be called at a later time when this new
reference to it. It may then be called at a later time when this new
reference is deleted.
Also note that i
t is not guaranteed that
reference is deleted.
I
t is not guaranteed that
\code
{__
del
__}
methods are called for objects that still exist when
\code
{__
del
__}
methods are called for objects that still exist when
the interpreter exits.
the interpreter exits.
Note that
\code
{
del x
}
doesn't directly call
\code
{
x.
__
del
__}
-- the
former decrements the reference count for
\code
{
x
}
by one, but
\code
{
x,
__
del
__}
is only called when its reference count reaches zero.
\item
[\tt __repr__(self)]
\item
[\tt __repr__(self)]
Called by the
\verb
@
repr()
@
built-in function and by conversions
Called by the
\verb
@
repr()
@
built-in function and by conversions
(reverse quotes) to compute the string representation of an object.
(reverse quotes) to compute the string representation of an object.
...
@@ -635,7 +639,8 @@ exceptions raised by comparisons are ignored, and the objects will be
...
@@ -635,7 +639,8 @@ exceptions raised by comparisons are ignored, and the objects will be
considered equal in this case.)
considered equal in this case.)
\item
[\tt __hash__(self)]
\item
[\tt __hash__(self)]
Called by dictionary operations and by the built-in function
Called for the key object for dictionary operations,
and by the built-in function
\code
{
hash()
}
. Should return a 32-bit integer usable as a hash value
\code
{
hash()
}
. Should return a 32-bit integer usable as a hash value
for dictionary operations. The only required property is that objects
for dictionary operations. The only required property is that objects
which compare equal have the same hash value; it is advised to somehow
which compare equal have the same hash value; it is advised to somehow
...
@@ -650,6 +655,50 @@ implements a \code{__cmp__} method it should not implement
...
@@ -650,6 +655,50 @@ implements a \code{__cmp__} method it should not implement
key's hash value is a constant.
key's hash value is a constant.
\obindex
{
dictionary
}
\obindex
{
dictionary
}
\item
[\tt __call__(self, *args)]
Called when the instance is ``called'' as a function.
\end{description}
\subsection
{
Special methods for attribute access
}
The following methods can be used to change the meaning of attribute
access for class instances.
\begin{description}
\item
[\tt __getattr__(self, name)]
Called when an attribute lookup has not found the attribute in the
usual places (i.e. it is not an instance attribute nor is it found in
the class tree for
\code
{
self
}
).
\code
{
name
}
is the attribute name.
Note that if the attribute is found through the normal mechanism,
\code
{__
getattr
__}
is not called. (This is an asymmetry between
\code
{__
getattr
__}
and
\code
{__
setattr
__}
.)
This is done both for efficiency reasons and because otherwise
\code
{__
getattr
__}
would have no way to access other attributes of the
instance.
Note that at least for instance variables,
\code
{__
getattr
__}
can fake
total control by simply not inserting any values in the instance
attribute dictionary.
\item
[\tt __setattr__(self, name, value)]
Called when an attribute assignment is attempted. This is called
instead of the normal mechanism (i.e. store the value as an instance
attribute).
\code
{
name
}
is the attribute name,
\code
{
value
}
is the
value to be assigned to it.
If
\code
{__
setattr
__}
wants to assign to an instance attribute, it
should not simply execute
\code
{
self.
\var
{
name
}
= value
}
-- this would
cause a recursive call. Instead, it should insert the value in the
dictionary of instance attributes, e.g.
\code
{
self.
__
dict
__
[name] =
value
}
.
\item
[\tt __delattr__(self, name)]
Like
\code
{__
setattr
__}
but for attribute deletion instead of
assignment.
\end{description}
\end{description}
...
...
Doc/ref3.tex
Dosyayı görüntüle @
29c1b97d
...
@@ -613,10 +613,14 @@ must explicitly call it to ensure proper deletion of the base class
...
@@ -613,10 +613,14 @@ must explicitly call it to ensure proper deletion of the base class
part of the instance. Note that it is possible for the
\code
{__
del
__}
part of the instance. Note that it is possible for the
\code
{__
del
__}
method to postpone destruction of the instance by creating a new
method to postpone destruction of the instance by creating a new
reference to it. It may then be called at a later time when this new
reference to it. It may then be called at a later time when this new
reference is deleted.
Also note that i
t is not guaranteed that
reference is deleted.
I
t is not guaranteed that
\code
{__
del
__}
methods are called for objects that still exist when
\code
{__
del
__}
methods are called for objects that still exist when
the interpreter exits.
the interpreter exits.
Note that
\code
{
del x
}
doesn't directly call
\code
{
x.
__
del
__}
-- the
former decrements the reference count for
\code
{
x
}
by one, but
\code
{
x,
__
del
__}
is only called when its reference count reaches zero.
\item
[\tt __repr__(self)]
\item
[\tt __repr__(self)]
Called by the
\verb
@
repr()
@
built-in function and by conversions
Called by the
\verb
@
repr()
@
built-in function and by conversions
(reverse quotes) to compute the string representation of an object.
(reverse quotes) to compute the string representation of an object.
...
@@ -635,7 +639,8 @@ exceptions raised by comparisons are ignored, and the objects will be
...
@@ -635,7 +639,8 @@ exceptions raised by comparisons are ignored, and the objects will be
considered equal in this case.)
considered equal in this case.)
\item
[\tt __hash__(self)]
\item
[\tt __hash__(self)]
Called by dictionary operations and by the built-in function
Called for the key object for dictionary operations,
and by the built-in function
\code
{
hash()
}
. Should return a 32-bit integer usable as a hash value
\code
{
hash()
}
. Should return a 32-bit integer usable as a hash value
for dictionary operations. The only required property is that objects
for dictionary operations. The only required property is that objects
which compare equal have the same hash value; it is advised to somehow
which compare equal have the same hash value; it is advised to somehow
...
@@ -650,6 +655,50 @@ implements a \code{__cmp__} method it should not implement
...
@@ -650,6 +655,50 @@ implements a \code{__cmp__} method it should not implement
key's hash value is a constant.
key's hash value is a constant.
\obindex
{
dictionary
}
\obindex
{
dictionary
}
\item
[\tt __call__(self, *args)]
Called when the instance is ``called'' as a function.
\end{description}
\subsection
{
Special methods for attribute access
}
The following methods can be used to change the meaning of attribute
access for class instances.
\begin{description}
\item
[\tt __getattr__(self, name)]
Called when an attribute lookup has not found the attribute in the
usual places (i.e. it is not an instance attribute nor is it found in
the class tree for
\code
{
self
}
).
\code
{
name
}
is the attribute name.
Note that if the attribute is found through the normal mechanism,
\code
{__
getattr
__}
is not called. (This is an asymmetry between
\code
{__
getattr
__}
and
\code
{__
setattr
__}
.)
This is done both for efficiency reasons and because otherwise
\code
{__
getattr
__}
would have no way to access other attributes of the
instance.
Note that at least for instance variables,
\code
{__
getattr
__}
can fake
total control by simply not inserting any values in the instance
attribute dictionary.
\item
[\tt __setattr__(self, name, value)]
Called when an attribute assignment is attempted. This is called
instead of the normal mechanism (i.e. store the value as an instance
attribute).
\code
{
name
}
is the attribute name,
\code
{
value
}
is the
value to be assigned to it.
If
\code
{__
setattr
__}
wants to assign to an instance attribute, it
should not simply execute
\code
{
self.
\var
{
name
}
= value
}
-- this would
cause a recursive call. Instead, it should insert the value in the
dictionary of instance attributes, e.g.
\code
{
self.
__
dict
__
[name] =
value
}
.
\item
[\tt __delattr__(self, name)]
Like
\code
{__
setattr
__}
but for attribute deletion instead of
assignment.
\end{description}
\end{description}
...
...
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