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
3abd13de
Commit
3abd13de
authored
Feb 28, 2003
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SF doc patch #692001, properties and __getattribute__. I added some
stuff, and changed 'property' to 'descriptor'.
parent
bcc9feb2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
8 deletions
+54
-8
Doc/ref/ref3.tex
Doc/ref/ref3.tex
+54
-8
No files found.
Doc/ref/ref3.tex
View file @
3abd13de
...
...
@@ -1155,9 +1155,6 @@ to Unicode using the system default encoding.
The following methods can be defined to customize the meaning of
attribute access (use of, assignment to, or deletion of
\code
{
x.name
}
)
for class instances.
For performance reasons, these methods are cached in the class object
at class definition time; therefore, they cannot be changed after the
class definition is executed.
\begin{methoddesc}
[object]
{__
getattr
__}{
self, name
}
Called when an attribute lookup has not found the attribute in the
...
...
@@ -1171,10 +1168,11 @@ Note that if the attribute is found through the normal mechanism,
asymmetry between
\method
{__
getattr
__
()
}
and
\method
{__
setattr
__
()
}
.)
This is done both for efficiency reasons and because otherwise
\method
{__
setattr
__
()
}
would have no way to access other attributes of
the instance.
Note that at least for instance variables, you can fake
total control by not inserting any values in the instance
attribute dictionary (but instead inserting them in another object).
the instance. Note that at least for instance variables, you can fake
total control by not inserting any values in the instance attribute
dictionary (but instead inserting them in another object). See the
\method
{__
getattribute
__
()
}
method below for a way to actually get
total control in new-style classes.
\withsubitem
{
(object method)
}{
\ttindex
{__
setattr
__
()
}}
\end{methoddesc}
...
...
@@ -1188,7 +1186,10 @@ If \method{__setattr__()} wants to assign to an instance attribute, it
should not simply execute
\samp
{
self.
\var
{
name
}
= value
}
--- this
would cause a recursive call to itself. Instead, it should insert the
value in the dictionary of instance attributes, e.g.,
\samp
{
self.
__
dict
__
[
\var
{
name
}
] = value
}
.
\samp
{
self.
__
dict
__
[
\var
{
name
}
] = value
}
. For new-style classes,
rather than accessing the instance dictionary, it should call the base
class method with the same name, for example,
\samp
{
object.
__
setattr
__
(self, name, value)
}
.
\withsubitem
{
(instance attribute)
}{
\ttindex
{__
dict
__}}
\end{methoddesc}
...
...
@@ -1198,6 +1199,51 @@ assignment. This should only be implemented if \samp{del
obj.
\var
{
name
}}
is meaningful for the object.
\end{methoddesc}
\subsubsection
{
More attribute access for new-style classes
\lable
{
new-style-attribute-access
}}
The following methods only apply to new-style classes.
\begin{methoddesc}
[object]
{__
getattribute
__}{
self, name
}
Called unconditionally to implement attribute accesses for instances
of the class. If the class also defines
\method
{__
getattr
__}
, it will
never be called (unless called explicitly).
This method should return the (computed) attribute
value or raise an
\exception
{
AttributeError
}
exception.
In order to avoid infinite recursion in this method, its
implementation should always call the base class method with the same
name to access any attributes it needs to access, for example,
\samp
{
object.
__
getattribute
__
(self, name)
}
.
\end{methoddesc}
\subsubsubsection
{
Implementing Descriptors
\label
{
descriptors
}}
The following methods only apply when an instance of the class
containing the method (a so-called
\emph
{
descriptor
}
class) is in
the class dictionary of another new-style class, known as the
\emph
{
owner
}
class. In the examples below, ``the attribute'' refers to
the attribute whose name is the key of the property in the accessed
class'
\code
{__
dict
__}
.
\begin{methoddesc}
[object]
{__
get
__}{
self, instance, owner
}
Called to get the attribute of the owner class (class attribute acess)
or of an instance of that class (instance attribute acces).
\var
{
owner
}
is always the owner class, while
\var
{
instance
}
is the
instance that the attribute was accessed through, or
\code
{
None
}
when
the attribute is accessed through the
\var
{
owner
}
. This method should
return the (computed) attribute value or raise an
\exception
{
AttributeError
}
exception.
\end{methoddesc}
\begin{methoddesc}
[object]
{__
set
__}{
self, instance, value
}
Called to set the attribute on an instance
\{
instance
}
of the owner
class to a new value,
\var
{
value
}
.
\end{methoddesc}
\begin{methoddesc}
[object]
{__
delete
__}{
self, instance
}
Called to delete the attribute on an instance
\{
instance
}
of the owner
class.
\end{methoddesc}
\subsection
{
Emulating callable objects
\label
{
callable-types
}}
...
...
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