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

Skip to content

Commit 29c1b97

Browse files
committed
__call__, __getattr__ c.s.
1 parent 2e61103 commit 29c1b97

2 files changed

Lines changed: 102 additions & 4 deletions

File tree

Doc/ref/ref3.tex

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,14 @@ \subsection{Special methods for any type}
613613
part of the instance. Note that it is possible for the \code{__del__}
614614
method to postpone destruction of the instance by creating a new
615615
reference to it. It may then be called at a later time when this new
616-
reference is deleted. Also note that it is not guaranteed that
616+
reference is deleted. It is not guaranteed that
617617
\code{__del__} methods are called for objects that still exist when
618618
the interpreter exits.
619619

620+
Note that \code{del x} doesn't directly call \code{x.__del__} -- the
621+
former decrements the reference count for \code{x} by one, but
622+
\code{x,__del__} is only called when its reference count reaches zero.
623+
620624
\item[\tt __repr__(self)]
621625
Called by the \verb@repr()@ built-in function and by conversions
622626
(reverse quotes) to compute the string representation of an object.
@@ -635,7 +639,8 @@ \subsection{Special methods for any type}
635639
considered equal in this case.)
636640

637641
\item[\tt __hash__(self)]
638-
Called by dictionary operations and by the built-in function
642+
Called for the key object for dictionary operations,
643+
and by the built-in function
639644
\code{hash()}. Should return a 32-bit integer usable as a hash value
640645
for dictionary operations. The only required property is that objects
641646
which compare equal have the same hash value; it is advised to somehow
@@ -650,6 +655,50 @@ \subsection{Special methods for any type}
650655
key's hash value is a constant.
651656
\obindex{dictionary}
652657

658+
\item[\tt __call__(self, *args)]
659+
Called when the instance is ``called'' as a function.
660+
661+
\end{description}
662+
663+
664+
\subsection{Special methods for attribute access}
665+
666+
The following methods can be used to change the meaning of attribute
667+
access for class instances.
668+
669+
\begin{description}
670+
671+
\item[\tt __getattr__(self, name)]
672+
Called when an attribute lookup has not found the attribute in the
673+
usual places (i.e. it is not an instance attribute nor is it found in
674+
the class tree for \code{self}). \code{name} is the attribute name.
675+
676+
Note that if the attribute is found through the normal mechanism,
677+
\code{__getattr__} is not called. (This is an asymmetry between
678+
\code{__getattr__} and \code{__setattr__}.)
679+
This is done both for efficiency reasons and because otherwise
680+
\code{__getattr__} would have no way to access other attributes of the
681+
instance.
682+
Note that at least for instance variables, \code{__getattr__} can fake
683+
total control by simply not inserting any values in the instance
684+
attribute dictionary.
685+
686+
\item[\tt __setattr__(self, name, value)]
687+
Called when an attribute assignment is attempted. This is called
688+
instead of the normal mechanism (i.e. store the value as an instance
689+
attribute). \code{name} is the attribute name, \code{value} is the
690+
value to be assigned to it.
691+
692+
If \code{__setattr__} wants to assign to an instance attribute, it
693+
should not simply execute \code{self.\var{name} = value} -- this would
694+
cause a recursive call. Instead, it should insert the value in the
695+
dictionary of instance attributes, e.g. \code{self.__dict__[name] =
696+
value}.
697+
698+
\item[\tt __delattr__(self, name)]
699+
Like \code{__setattr__} but for attribute deletion instead of
700+
assignment.
701+
653702
\end{description}
654703

655704

Doc/ref3.tex

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,14 @@ \subsection{Special methods for any type}
613613
part of the instance. Note that it is possible for the \code{__del__}
614614
method to postpone destruction of the instance by creating a new
615615
reference to it. It may then be called at a later time when this new
616-
reference is deleted. Also note that it is not guaranteed that
616+
reference is deleted. It is not guaranteed that
617617
\code{__del__} methods are called for objects that still exist when
618618
the interpreter exits.
619619

620+
Note that \code{del x} doesn't directly call \code{x.__del__} -- the
621+
former decrements the reference count for \code{x} by one, but
622+
\code{x,__del__} is only called when its reference count reaches zero.
623+
620624
\item[\tt __repr__(self)]
621625
Called by the \verb@repr()@ built-in function and by conversions
622626
(reverse quotes) to compute the string representation of an object.
@@ -635,7 +639,8 @@ \subsection{Special methods for any type}
635639
considered equal in this case.)
636640

637641
\item[\tt __hash__(self)]
638-
Called by dictionary operations and by the built-in function
642+
Called for the key object for dictionary operations,
643+
and by the built-in function
639644
\code{hash()}. Should return a 32-bit integer usable as a hash value
640645
for dictionary operations. The only required property is that objects
641646
which compare equal have the same hash value; it is advised to somehow
@@ -650,6 +655,50 @@ \subsection{Special methods for any type}
650655
key's hash value is a constant.
651656
\obindex{dictionary}
652657

658+
\item[\tt __call__(self, *args)]
659+
Called when the instance is ``called'' as a function.
660+
661+
\end{description}
662+
663+
664+
\subsection{Special methods for attribute access}
665+
666+
The following methods can be used to change the meaning of attribute
667+
access for class instances.
668+
669+
\begin{description}
670+
671+
\item[\tt __getattr__(self, name)]
672+
Called when an attribute lookup has not found the attribute in the
673+
usual places (i.e. it is not an instance attribute nor is it found in
674+
the class tree for \code{self}). \code{name} is the attribute name.
675+
676+
Note that if the attribute is found through the normal mechanism,
677+
\code{__getattr__} is not called. (This is an asymmetry between
678+
\code{__getattr__} and \code{__setattr__}.)
679+
This is done both for efficiency reasons and because otherwise
680+
\code{__getattr__} would have no way to access other attributes of the
681+
instance.
682+
Note that at least for instance variables, \code{__getattr__} can fake
683+
total control by simply not inserting any values in the instance
684+
attribute dictionary.
685+
686+
\item[\tt __setattr__(self, name, value)]
687+
Called when an attribute assignment is attempted. This is called
688+
instead of the normal mechanism (i.e. store the value as an instance
689+
attribute). \code{name} is the attribute name, \code{value} is the
690+
value to be assigned to it.
691+
692+
If \code{__setattr__} wants to assign to an instance attribute, it
693+
should not simply execute \code{self.\var{name} = value} -- this would
694+
cause a recursive call. Instead, it should insert the value in the
695+
dictionary of instance attributes, e.g. \code{self.__dict__[name] =
696+
value}.
697+
698+
\item[\tt __delattr__(self, name)]
699+
Like \code{__setattr__} but for attribute deletion instead of
700+
assignment.
701+
653702
\end{description}
654703

655704

0 commit comments

Comments
 (0)