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

Skip to content

Commit 2e0b755

Browse files
committed
Futher update docs after unbound method removal.
1 parent ff73795 commit 2e0b755

8 files changed

Lines changed: 99 additions & 125 deletions

File tree

Doc/c-api/concrete.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2600,8 +2600,9 @@ There are some useful functions that are useful for working with method objects.
26002600
function that will be called when the method is called. If this method should
26012601
be bound to an instance, *self* should be the instance and *class* should be the
26022602
class of *self*, otherwise *self* should be *NULL* and *class* should be the
2603-
class which provides the unbound method..
2603+
class which provides the unbound method.
26042604

2605+
.. XXX no unbound methods anymore...
26052606
26062607
.. cfunction:: PyObject* PyMethod_Class(PyObject *meth)
26072608

Doc/distutils/apiref.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,12 +1965,12 @@ Subclasses of :class:`Command` must define the following methods.
19651965
as the parent with sub-commands ``install_lib``, ``install_headers``, etc. The
19661966
parent of a family of commands defines *sub_commands* as a class attribute; it's
19671967
a list of 2-tuples ``(command_name, predicate)``, with *command_name* a string
1968-
and *predicate* an unbound method, a string or None. *predicate* is a method of
1968+
and *predicate* a function, a string or None. *predicate* is a method of
19691969
the parent command that determines whether the corresponding command is
19701970
applicable in the current situation. (Eg. we ``install_headers`` is only
19711971
applicable if we have any C header files to install.) If *predicate* is None,
19721972
that command is always applicable.
19731973

19741974
*sub_commands* is usually defined at the \*end\* of a class, because predicates
1975-
can be unbound methods, so they must already have been defined. The canonical
1976-
example is the :command:`install` command.
1975+
can be methods of the class, so they must already have been defined. The
1976+
canonical example is the :command:`install` command.

Doc/library/new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ The :mod:`new` module defines the following functions:
2222
This function will return a method object, bound to *instance*.
2323
*function* must be callable.
2424

25+
.. XXX no unbound methods anymore
26+
2527
2628
.. function:: function(code, globals[, name[, argdefs[, closure]]])
2729

Doc/library/operator.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ objects.
390390
Use the :func:`callable` built-in function instead.
391391

392392
Returns true if the object *obj* can be called like a function, otherwise it
393-
returns false. True is returned for functions, bound and unbound methods, class
393+
returns false. True is returned for functions, instance methods, class
394394
objects, and instance objects which support the :meth:`__call__` method.
395395

396396

Doc/library/stdtypes.rst

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,23 +2215,19 @@ two flavors: built-in methods (such as :meth:`append` on lists) and class
22152215
instance methods. Built-in methods are described with the types that support
22162216
them.
22172217

2218-
The implementation adds two special read-only attributes to class instance
2219-
methods: ``m.__self__`` is the object on which the method operates, and
2220-
``m.__func__`` is the function implementing the method. Calling ``m(arg-1,
2221-
arg-2, ..., arg-n)`` is completely equivalent to calling ``m.__func__(
2222-
m.__self__, arg-1, arg-2, ..., arg-n)``.
2223-
2224-
Class instance methods are either *bound* or *unbound*, referring to whether the
2225-
method was accessed through an instance or a class, respectively. When a method
2226-
is unbound, its ``__self__`` attribute will be ``None`` and if called, an
2227-
explicit ``self`` object must be passed as the first argument. In this case,
2228-
``self`` must be an instance of the unbound method's class (or a subclass of
2229-
that class), otherwise a :exc:`TypeError` is raised.
2230-
2231-
Like function objects, methods objects support getting arbitrary attributes.
2232-
However, since method attributes are actually stored on the underlying function
2233-
object (``meth.__func__``), setting method attributes on either bound or unbound
2234-
methods is disallowed. Attempting to set a method attribute results in a
2218+
If you access a method (a function defined in a class namespace) through an
2219+
instance, you get a special object: a :dfn:`bound method` (also called
2220+
:dfn:`instance method`) object. When called, it will add the ``self`` argument
2221+
to the argument list. Bound methods have two special read-only attributes:
2222+
``m.__self__`` is the object on which the method operates, and ``m.__func__`` is
2223+
the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)``
2224+
is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ...,
2225+
arg-n)``.
2226+
2227+
Like function objects, bound method objects support getting arbitrary
2228+
attributes. However, since method attributes are actually stored on the
2229+
underlying function object (``meth.__func__``), setting method attributes on
2230+
bound methods is disallowed. Attempting to set a method attribute results in a
22352231
:exc:`TypeError` being raised. In order to set a method attribute, you need to
22362232
explicitly set it on the underlying function object::
22372233

Doc/library/weakref.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ directly. The low-level machinery used by the weak dictionary implementations
5050
is exposed by the :mod:`weakref` module for the benefit of advanced uses.
5151

5252
Not all objects can be weakly referenced; those objects which can include class
53-
instances, functions written in Python (but not in C), methods (both bound and
54-
unbound), sets, frozensets, file objects, :term:`generator`\s, type objects,
55-
:class:`DBcursor` objects from the :mod:`bsddb` module, sockets, arrays, deques,
56-
and regular expression pattern objects.
53+
instances, functions written in Python (but not in C), instance methods, sets,
54+
frozensets, file objects, :term:`generator`\s, type objects, :class:`DBcursor`
55+
objects from the :mod:`bsddb` module, sockets, arrays, deques, and regular
56+
expression pattern objects.
5757

5858
Several builtin types such as :class:`list` and :class:`dict` do not directly
5959
support weak references but can add support through subclassing::

Doc/reference/datamodel.rst

Lines changed: 72 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -529,92 +529,74 @@ Callable types
529529
single: __kwdefaults__ (function attribute)
530530
pair: global; namespace
531531

532-
User-defined methods
532+
Instance methods
533533
.. index::
534534
object: method
535535
object: user-defined method
536536
pair: user-defined; method
537537

538-
A user-defined method object combines a class, a class instance (or ``None``)
539-
and any callable object (normally a user-defined function).
538+
An instance method object combines a class, a class instance and any
539+
callable object (normally a user-defined function).
540+
541+
.. index::
542+
single: __func__ (method attribute)
543+
single: __self__ (method attribute)
544+
single: __doc__ (method attribute)
545+
single: __name__ (method attribute)
546+
single: __module__ (method attribute)
540547

541548
Special read-only attributes: :attr:`__self__` is the class instance object,
542549
:attr:`__func__` is the function object; :attr:`__doc__` is the method's
543550
documentation (same as ``__func__.__doc__``); :attr:`__name__` is the
544551
method name (same as ``__func__.__name__``); :attr:`__module__` is the
545552
name of the module the method was defined in, or ``None`` if unavailable.
546553

547-
.. index::
548-
single: __doc__ (method attribute)
549-
single: __name__ (method attribute)
550-
single: __module__ (method attribute)
551-
single: __func__ (method attribute)
552-
single: __self__ (method attribute)
553-
554554
Methods also support accessing (but not setting) the arbitrary function
555555
attributes on the underlying function object.
556556

557-
User-defined method objects may be created when getting an attribute of a class
558-
(perhaps via an instance of that class), if that attribute is a user-defined
559-
function object, an unbound user-defined method object, or a class method
560-
object. When the attribute is a user-defined method object, a new method object
561-
is only created if the class from which it is being retrieved is the same as, or
562-
a derived class of, the class stored in the original method object; otherwise,
563-
the original method object is used as it is.
564-
565-
.. index::
566-
single: __func__ (method attribute)
567-
single: __self__ (method attribute)
568-
569-
When a user-defined method object is created by retrieving a user-defined
570-
function object from a class, its :attr:`__self__` attribute is ``None``
571-
and the method object is said to be unbound. When one is created by
572-
retrieving a user-defined function object from a class via one of its
573-
instances, its :attr:`__self__` attribute is the instance, and the method
574-
object is said to be bound. Its :attr:`__func__` attribute is the
575-
original function object.
576-
577-
.. index:: single: __func__ (method attribute)
578-
579-
When a user-defined method object is created by retrieving another method object
580-
from a class or instance, the behaviour is the same as for a function object,
581-
except that the :attr:`__func__` attribute of the new instance is not the
582-
original method object but its :attr:`__func__` attribute.
583-
584-
.. index::
585-
single: __func__ (method attribute)
586-
single: __self__ (method attribute)
587-
588-
When a user-defined method object is created by retrieving a class method object
589-
from a class or instance, its :attr:`__self__` attribute is the class itself (the
590-
same as the :attr:`im_class` attribute), and its :attr:`__func__` attribute is
591-
the function object underlying the class method.
592-
593-
When an unbound user-defined method object is called, the underlying function
594-
(:attr:`__func__`) is called, with the restriction that the first argument must
595-
be an instance of the proper class (:attr:`im_class`) or of a derived class
596-
thereof.
597-
598-
When a bound user-defined method object is called, the underlying function
599-
(:attr:`__func__`) is called, inserting the class instance (:attr:`__self__`) in
600-
front of the argument list. For instance, when :class:`C` is a class which
601-
contains a definition for a function :meth:`f`, and ``x`` is an instance of
602-
:class:`C`, calling ``x.f(1)`` is equivalent to calling ``C.f(x, 1)``.
603-
604-
When a user-defined method object is derived from a class method object, the
605-
"class instance" stored in :attr:`__self__` will actually be the class itself, so
606-
that calling either ``x.f(1)`` or ``C.f(1)`` is equivalent to calling ``f(C,1)``
607-
where ``f`` is the underlying function.
608-
609-
Note that the transformation from function object to (unbound or bound) method
610-
object happens each time the attribute is retrieved from the class or instance.
611-
In some cases, a fruitful optimization is to assign the attribute to a local
612-
variable and call that local variable. Also notice that this transformation only
613-
happens for user-defined functions; other callable objects (and all non-callable
614-
objects) are retrieved without transformation. It is also important to note
615-
that user-defined functions which are attributes of a class instance are not
616-
converted to bound methods; this *only* happens when the function is an
617-
attribute of the class.
557+
User-defined method objects may be created when getting an attribute of a
558+
class (perhaps via an instance of that class), if that attribute is a
559+
user-defined function object or a class method object.
560+
561+
When an instance method object is created by retrieving a user-defined
562+
function object from a class via one of its instances, its
563+
:attr:`__self__` attribute is the instance, and the method object is said
564+
to be bound. The new method's :attr:`__func__` attribute is the original
565+
function object.
566+
567+
When a user-defined method object is created by retrieving another method
568+
object from a class or instance, the behaviour is the same as for a
569+
function object, except that the :attr:`__func__` attribute of the new
570+
instance is not the original method object but its :attr:`__func__`
571+
attribute.
572+
573+
When an instance method object is created by retrieving a class method
574+
object from a class or instance, its :attr:`__self__` attribute is the
575+
class itself, and its :attr:`__func__` attribute is the function object
576+
underlying the class method.
577+
578+
When an instance method object is called, the underlying function
579+
(:attr:`__func__`) is called, inserting the class instance
580+
(:attr:`__self__`) in front of the argument list. For instance, when
581+
:class:`C` is a class which contains a definition for a function
582+
:meth:`f`, and ``x`` is an instance of :class:`C`, calling ``x.f(1)`` is
583+
equivalent to calling ``C.f(x, 1)``.
584+
585+
When an instance method object is derived from a class method object, the
586+
"class instance" stored in :attr:`__self__` will actually be the class
587+
itself, so that calling either ``x.f(1)`` or ``C.f(1)`` is equivalent to
588+
calling ``f(C,1)`` where ``f`` is the underlying function.
589+
590+
Note that the transformation from function object to instance method
591+
object happens each time the attribute is retrieved from the instance. In
592+
some cases, a fruitful optimization is to assign the attribute to a local
593+
variable and call that local variable. Also notice that this
594+
transformation only happens for user-defined functions; other callable
595+
objects (and all non-callable objects) are retrieved without
596+
transformation. It is also important to note that user-defined functions
597+
which are attributes of a class instance are not converted to bound
598+
methods; this *only* happens when the function is an attribute of the
599+
class.
618600

619601
Generator functions
620602
.. index::
@@ -731,16 +713,12 @@ Custom classes
731713
pair: class; attribute
732714

733715
When a class attribute reference (for class :class:`C`, say) would yield a
734-
user-defined function object or an unbound user-defined method object whose
735-
associated class is either :class:`C` or one of its base classes, it is
736-
transformed into an unbound user-defined method object whose :attr:`im_class`
737-
attribute is :class:`C`. When it would yield a class method object, it is
738-
transformed into a bound user-defined method object whose :attr:`im_class`
739-
and :attr:`__self__` attributes are both :class:`C`. When it would yield a
740-
static method object, it is transformed into the object wrapped by the static
741-
method object. See section :ref:`descriptors` for another way in which
742-
attributes retrieved from a class may differ from those actually contained in
743-
its :attr:`__dict__`.
716+
class method object, it is transformed into an instance method object whose
717+
:attr:`__self__` attributes is :class:`C`. When it would yield a static
718+
method object, it is transformed into the object wrapped by the static method
719+
object. See section :ref:`descriptors` for another way in which attributes
720+
retrieved from a class may differ from those actually contained in its
721+
:attr:`__dict__`.
744722

745723
.. index:: triple: class; attribute; assignment
746724

@@ -772,22 +750,19 @@ Class instances
772750
pair: class; instance
773751
pair: class instance; attribute
774752

775-
A class instance is created by calling a class object (see above). A class
776-
instance has a namespace implemented as a dictionary which is the first place in
777-
which attribute references are searched. When an attribute is not found there,
778-
and the instance's class has an attribute by that name, the search continues
779-
with the class attributes. If a class attribute is found that is a user-defined
780-
function object or an unbound user-defined method object whose associated class
781-
is the class (call it :class:`C`) of the instance for which the attribute
782-
reference was initiated or one of its bases, it is transformed into a bound
783-
user-defined method object whose :attr:`im_class` attribute is :class:`C` and
784-
whose :attr:`__self__` attribute is the instance. Static method and class method
785-
objects are also transformed, as if they had been retrieved from class
786-
:class:`C`; see above under "Classes". See section :ref:`descriptors` for
787-
another way in which attributes of a class retrieved via its instances may
788-
differ from the objects actually stored in the class's :attr:`__dict__`. If no
789-
class attribute is found, and the object's class has a :meth:`__getattr__`
790-
method, that is called to satisfy the lookup.
753+
A class instance is created by calling a class object (see above). A class
754+
instance has a namespace implemented as a dictionary which is the first place
755+
in which attribute references are searched. When an attribute is not found
756+
there, and the instance's class has an attribute by that name, the search
757+
continues with the class attributes. If a class attribute is found that is a
758+
user-defined function object, it is transformed into an instance method
759+
object whose :attr:`__self__` attribute is the instance. Static method and
760+
class method objects are also transformed; see above under "Classes". See
761+
section :ref:`descriptors` for another way in which attributes of a class
762+
retrieved via its instances may differ from the objects actually stored in
763+
the class's :attr:`__dict__`. If no class attribute is found, and the
764+
object's class has a :meth:`__getattr__` method, that is called to satisfy
765+
the lookup.
791766

792767
.. index:: triple: class instance; attribute; assignment
793768

Tools/bgen/bgen/bgenObjectDefinition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ class PEP252Mixin:
236236
def assertions(self):
237237
# Check that various things aren't overridden. If they are it could
238238
# signify a bgen-client that has been partially converted to PEP252.
239-
assert self.outputGetattr.im_func == PEP252Mixin.outputGetattr.im_func
240-
assert self.outputSetattr.im_func == PEP252Mixin.outputSetattr.im_func
239+
assert self.outputGetattr.__func__ == PEP252Mixin.outputGetattr.__func__
240+
assert self.outputSetattr.__func__ == PEP252Mixin.outputSetattr.__func__
241241
assert self.outputGetattrBody == None
242242
assert self.outputGetattrHook == None
243243
assert self.basechain == "NULL"

0 commit comments

Comments
 (0)