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
d4614e83
Commit
d4614e83
authored
Feb 27, 2001
by
Barry Warsaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updates to the semantics of function and method attributes.
parent
ab354bb9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
14 deletions
+18
-14
Doc/lib/libstdtypes.tex
Doc/lib/libstdtypes.tex
+18
-14
No files found.
Doc/lib/libstdtypes.tex
View file @
d4614e83
...
@@ -982,6 +982,16 @@ attributes. \emph{Note that the current implementation only supports
...
@@ -982,6 +982,16 @@ attributes. \emph{Note that the current implementation only supports
function attributes on functions written in Python. Function
function attributes on functions written in Python. Function
attributes on built-ins may be supported in the future.
}
attributes on built-ins may be supported in the future.
}
Functions have another special attribute
\code
{
\var
{
f
}
.
__
dict
__}
(a.k.a.
\code
{
\var
{
f
}
.func
_
dict
}
) which contains the namespace used to
support function attributes.
\code
{__
dict
__}
can be accessed
directly, set to a dictionary object, or
\code
{
None
}
. It can also be
deleted (but the following two lines are equivalent):
\begin{verbatim}
del func.
__
dict
__
func.
__
dict
__
= None
\end{verbatim}
\subsubsection
{
Methods
\label
{
typesmethods
}}
\subsubsection
{
Methods
\label
{
typesmethods
}}
\obindex
{
method
}
\obindex
{
method
}
...
@@ -1007,14 +1017,13 @@ object must be passed as the first argument. In this case,
...
@@ -1007,14 +1017,13 @@ object must be passed as the first argument. In this case,
\code
{
self
}
must be an instance of the unbound method's class (or a
\code
{
self
}
must be an instance of the unbound method's class (or a
subclass of that class), otherwise a
\code
{
TypeError
}
is raised.
subclass of that class), otherwise a
\code
{
TypeError
}
is raised.
Like function objects, methods objects support getting and setting
Like function objects, methods objects support getting
arbitrary attributes. However, the attributes are actually stored on
arbitrary attributes. However, since method attributes are actually
the underlying function object (i.e.
\code
{
meth.im
_
func
}
). To avoid
stored on the underlying function object (i.e.
\code
{
meth.im
_
func
}
),
surprising behavior, a
\code
{
TypeError
}
is raised when an attempt is
setting method attributes on either bound or unbound methods is
made to set an attribute on a bound method. It is legal to get a
disallowed. Attempting to set a method attribute results in a
bound method's attribute (the underlying function's attribute is
\code
{
TypeError
}
being raised. In order to set a method attribute,
returned), and it is also legal to set or get an unbound method's
you need to explicitly set it on the underlying function object:
attribute. For example:
\begin{verbatim}
\begin{verbatim}
class C:
class C:
...
@@ -1022,14 +1031,9 @@ class C:
...
@@ -1022,14 +1031,9 @@ class C:
pass
pass
c = C()
c = C()
d = C()
c.method.im
_
func.whoami = 'my name is c'
c.method.whoami = 'my name is c'
d.method.whoami = 'my name is d'
\end{verbatim}
\end{verbatim}
If bound method attribute setting was allowed,
\code
{
c.method.whoami
}
would return ``my name is d''.
See the
\citetitle
[../ref/ref.html]
{
Python Reference Manual
}
for more
See the
\citetitle
[../ref/ref.html]
{
Python Reference Manual
}
for more
information.
information.
...
...
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