mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Also removed im_class and renamed im_self to __self__ and im_func to __func__. im_class can be substituted with method.__self__.__class__. I've also updated some parts of the documenation.
This commit is contained in:
parent
0d3fb8a944
commit
ff737954f3
23 changed files with 152 additions and 268 deletions
|
@ -49,14 +49,11 @@ attributes:
|
|||
| | __name__ | name with which this |
|
||||
| | | method was defined |
|
||||
+-----------+-----------------+---------------------------+
|
||||
| | im_class | class object that asked |
|
||||
| | | for this method |
|
||||
+-----------+-----------------+---------------------------+
|
||||
| | im_func | function object |
|
||||
| | __func__ | function object |
|
||||
| | | containing implementation |
|
||||
| | | of method |
|
||||
+-----------+-----------------+---------------------------+
|
||||
| | im_self | instance to which this |
|
||||
| | __self__ | instance to which this |
|
||||
| | | method is bound, or |
|
||||
| | | ``None`` |
|
||||
+-----------+-----------------+---------------------------+
|
||||
|
@ -264,7 +261,7 @@ attributes:
|
|||
Methods implemented via descriptors that also pass one of the other tests
|
||||
return false from the :func:`ismethoddescriptor` test, simply because the
|
||||
other tests promise more -- you can, e.g., count on having the
|
||||
:attr:`im_func` attribute (etc) when an object passes :func:`ismethod`.
|
||||
:attr:`__func__` attribute (etc) when an object passes :func:`ismethod`.
|
||||
|
||||
|
||||
.. function:: isdatadescriptor(object)
|
||||
|
|
|
@ -17,10 +17,10 @@ non-sensical arguments which crash the interpreter when the object is used.
|
|||
The :mod:`new` module defines the following functions:
|
||||
|
||||
|
||||
.. function:: instancemethod(function, instance, class)
|
||||
.. function:: instancemethod(function, instance)
|
||||
|
||||
This function will return a method object, bound to *instance*, or unbound if
|
||||
*instance* is ``None``. *function* must be callable.
|
||||
This function will return a method object, bound to *instance*.
|
||||
*function* must be callable.
|
||||
|
||||
|
||||
.. function:: function(code, globals[, name[, argdefs[, closure]]])
|
||||
|
|
|
@ -2216,21 +2216,21 @@ instance methods. Built-in methods are described with the types that support
|
|||
them.
|
||||
|
||||
The implementation adds two special read-only attributes to class instance
|
||||
methods: ``m.im_self`` is the object on which the method operates, and
|
||||
``m.im_func`` is the function implementing the method. Calling ``m(arg-1,
|
||||
arg-2, ..., arg-n)`` is completely equivalent to calling ``m.im_func(m.im_self,
|
||||
arg-1, arg-2, ..., arg-n)``.
|
||||
methods: ``m.__self__`` is the object on which the method operates, and
|
||||
``m.__func__`` is the function implementing the method. Calling ``m(arg-1,
|
||||
arg-2, ..., arg-n)`` is completely equivalent to calling ``m.__func__(
|
||||
m.__self__, arg-1, arg-2, ..., arg-n)``.
|
||||
|
||||
Class instance methods are either *bound* or *unbound*, referring to whether the
|
||||
method was accessed through an instance or a class, respectively. When a method
|
||||
is unbound, its ``im_self`` attribute will be ``None`` and if called, an
|
||||
is unbound, its ``__self__`` attribute will be ``None`` and if called, an
|
||||
explicit ``self`` object must be passed as the first argument. In this case,
|
||||
``self`` must be an instance of the unbound method's class (or a subclass of
|
||||
that class), otherwise a :exc:`TypeError` is raised.
|
||||
|
||||
Like function objects, methods objects support getting arbitrary attributes.
|
||||
However, since method attributes are actually stored on the underlying function
|
||||
object (``meth.im_func``), setting method attributes on either bound or unbound
|
||||
object (``meth.__func__``), setting method attributes on either bound or unbound
|
||||
methods is disallowed. Attempting to set a method attribute results in a
|
||||
:exc:`TypeError` being raised. In order to set a method attribute, you need to
|
||||
explicitly set it on the underlying function object::
|
||||
|
@ -2240,7 +2240,7 @@ explicitly set it on the underlying function object::
|
|||
pass
|
||||
|
||||
c = C()
|
||||
c.method.im_func.whoami = 'my name is c'
|
||||
c.method.__func__.whoami = 'my name is c'
|
||||
|
||||
See :ref:`types` for more information.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue