Commit d4614e83 authored by Barry Warsaw's avatar Barry Warsaw

Updates to the semantics of function and method attributes.

parent ab354bb9
...@@ -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.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment