| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -492,7 +492,7 @@ attributes (see :ref:`import-mod-attrs` for module attributes): | |||
| 492 | 492 | Methods implemented via descriptors that also pass one of the other tests | |
| 493 | 493 | return ``False`` from the :func:`ismethoddescriptor` test, simply because the | |
| 494 | 494 | other tests promise more -- you can, e.g., count on having the | |
| 495 | - :ref:`__func__ <instance-methods>` attribute (etc) when an object passes | ||
| 495 | + :attr:`~method.__func__` attribute (etc) when an object passes | ||
| 496 | 496 | :func:`ismethod`. | |
| 497 | 497 | ||
| 498 | 498 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5325,25 +5325,30 @@ Methods | |||
| 5325 | 5325 | .. index:: pair: object; method | |
| 5326 | 5326 | ||
| 5327 | 5327 | Methods are functions that are called using the attribute notation. There are | |
| 5328 | - two flavors: built-in methods (such as :meth:`append` on lists) and class | ||
| 5329 | - instance methods. Built-in methods are described with the types that support | ||
| 5330 | - them. | ||
| 5328 | + two flavors: :ref:`built-in methods <builtin-methods>` (such as :meth:`append` | ||
| 5329 | + on lists) and :ref:`class instance method <instance-methods>`. | ||
| 5330 | + Built-in methods are described with the types that support them. | ||
| 5331 | 5331 | ||
| 5332 | 5332 | If you access a method (a function defined in a class namespace) through an | |
| 5333 | 5333 | instance, you get a special object: a :dfn:`bound method` (also called | |
| 5334 | - :dfn:`instance method`) object. When called, it will add the ``self`` argument | ||
| 5334 | + :ref:`instance method <instance-methods>`) object. When called, it will add | ||
| 5335 | + the ``self`` argument | ||
| 5335 | 5336 | to the argument list. Bound methods have two special read-only attributes: | |
| 5336 | - ``m.__self__`` is the object on which the method operates, and ``m.__func__`` is | ||
| 5337 | + :attr:`m.__self__ <method.__self__>` is the object on which the method | ||
| 5338 | + operates, and :attr:`m.__func__ <method.__func__>` is | ||
| 5337 | 5339 | the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)`` | |
| 5338 | 5340 | is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ..., | |
| 5339 | 5341 | arg-n)``. | |
| 5340 | 5342 | ||
| 5341 | - Like function objects, bound method objects support getting arbitrary | ||
| 5343 | + Like :ref:`function objects <user-defined-funcs>`, bound method objects support | ||
| 5344 | + getting arbitrary | ||
| 5342 | 5345 | attributes. However, since method attributes are actually stored on the | |
| 5343 | - underlying function object (``meth.__func__``), setting method attributes on | ||
| 5346 | + underlying function object (:attr:`method.__func__`), setting method attributes on | ||
| 5344 | 5347 | bound methods is disallowed. Attempting to set an attribute on a method | |
| 5345 | 5348 | results in an :exc:`AttributeError` being raised. In order to set a method | |
| 5346 | - attribute, you need to explicitly set it on the underlying function object:: | ||
| 5349 | + attribute, you need to explicitly set it on the underlying function object: | ||
| 5350 | + | ||
| 5351 | + .. doctest:: | ||
| 5347 | 5352 | ||
| 5348 | 5353 | >>> class C: | |
| 5349 | 5354 | ... def method(self): | |
@@ -5358,7 +5363,7 @@ attribute, you need to explicitly set it on the underlying function object:: | |||
| 5358 | 5363 | >>> c.method.whoami | |
| 5359 | 5364 | 'my name is method' | |
| 5360 | 5365 | ||
| 5361 | - See :ref:`types` for more information. | ||
| 5366 | + See :ref:`instance-methods` for more information. | ||
| 5362 | 5367 | ||
| 5363 | 5368 | ||
| 5364 | 5369 | .. index:: object; code, code object | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -519,6 +519,8 @@ These are the types to which the function call operation (see section | |||
| 519 | 519 | :ref:`calls`) can be applied: | |
| 520 | 520 | ||
| 521 | 521 | ||
| 522 | + .. _user-defined-funcs: | ||
| 523 | + | ||
| 522 | 524 | User-defined functions | |
| 523 | 525 | ^^^^^^^^^^^^^^^^^^^^^^ | |
| 524 | 526 | ||
@@ -654,43 +656,64 @@ callable object (normally a user-defined function). | |||
| 654 | 656 | single: __name__ (method attribute) | |
| 655 | 657 | single: __module__ (method attribute) | |
| 656 | 658 | ||
| 657 | - Special read-only attributes: :attr:`__self__` is the class instance object, | ||
| 658 | - :attr:`__func__` is the function object; :attr:`__doc__` is the method's | ||
| 659 | - documentation (same as ``__func__.__doc__``); :attr:`~definition.__name__` is the | ||
| 660 | - method name (same as ``__func__.__name__``); :attr:`__module__` is the | ||
| 661 | - name of the module the method was defined in, or ``None`` if unavailable. | ||
| 659 | + Special read-only attributes: | ||
| 660 | + | ||
| 661 | + .. list-table:: | ||
| 662 | + | ||
| 663 | + * - .. attribute:: method.__self__ | ||
| 664 | + - Refers to the class instance object to which the method is | ||
| 665 | + :ref:`bound <method-binding>` | ||
| 666 | + | ||
| 667 | + * - .. attribute:: method.__func__ | ||
| 668 | + - Refers to the original function object | ||
| 669 | + | ||
| 670 | + * - .. attribute:: method.__doc__ | ||
| 671 | + - The method's documentation (same as :attr:`!method.__func__.__doc__`). | ||
| 672 | + A :class:`string <str>` if the original function had a docstring, else | ||
| 673 | + ``None``. | ||
| 674 | + | ||
| 675 | + * - .. attribute:: method.__name__ | ||
| 676 | + - The name of the method (same as :attr:`!method.__func__.__name__`) | ||
| 677 | + | ||
| 678 | + * - .. attribute:: method.__module__ | ||
| 679 | + - The name of the module the method was defined in, or ``None`` if | ||
| 680 | + unavailable. | ||
| 662 | 681 | ||
| 663 | 682 | Methods also support accessing (but not setting) the arbitrary function | |
| 664 | - attributes on the underlying function object. | ||
| 683 | + attributes on the underlying :ref:`function object <user-defined-funcs>`. | ||
| 665 | 684 | ||
| 666 | 685 | User-defined method objects may be created when getting an attribute of a | |
| 667 | 686 | class (perhaps via an instance of that class), if that attribute is a | |
| 668 | - user-defined function object or a class method object. | ||
| 687 | + user-defined :ref:`function object <user-defined-funcs>` or a | ||
| 688 | + :class:`classmethod` object. | ||
| 689 | + | ||
| 690 | + .. _method-binding: | ||
| 669 | 691 | ||
| 670 | 692 | When an instance method object is created by retrieving a user-defined | |
| 671 | - function object from a class via one of its instances, its | ||
| 672 | - :attr:`__self__` attribute is the instance, and the method object is said | ||
| 673 | - to be bound. The new method's :attr:`__func__` attribute is the original | ||
| 674 | - function object. | ||
| 675 | - | ||
| 676 | - When an instance method object is created by retrieving a class method | ||
| 677 | - object from a class or instance, its :attr:`__self__` attribute is the | ||
| 678 | - class itself, and its :attr:`__func__` attribute is the function object | ||
| 693 | + :ref:`function object <user-defined-funcs>` from a class via one of its | ||
| 694 | + instances, its :attr:`~method.__self__` attribute is the instance, and the | ||
| 695 | + method object is said to be *bound*. The new method's :attr:`~method.__func__` | ||
| 696 | + attribute is the original function object. | ||
| 697 | + | ||
| 698 | + When an instance method object is created by retrieving a :class:`classmethod` | ||
| 699 | + object from a class or instance, its :attr:`~method.__self__` attribute is the | ||
| 700 | + class itself, and its :attr:`~method.__func__` attribute is the function object | ||
| 679 | 701 | underlying the class method. | |
| 680 | 702 | ||
| 681 | 703 | When an instance method object is called, the underlying function | |
| 682 | - (:attr:`__func__`) is called, inserting the class instance | ||
| 683 | - (:attr:`__self__`) in front of the argument list. For instance, when | ||
| 704 | + (:attr:`~method.__func__`) is called, inserting the class instance | ||
| 705 | + (:attr:`~method.__self__`) in front of the argument list. For instance, when | ||
| 684 | 706 | :class:`!C` is a class which contains a definition for a function | |
| 685 | 707 | :meth:`!f`, and ``x`` is an instance of :class:`!C`, calling ``x.f(1)`` is | |
| 686 | 708 | equivalent to calling ``C.f(x, 1)``. | |
| 687 | 709 | ||
| 688 | - When an instance method object is derived from a class method object, the | ||
| 689 | - "class instance" stored in :attr:`__self__` will actually be the class | ||
| 710 | + When an instance method object is derived from a :class:`classmethod` object, the | ||
| 711 | + "class instance" stored in :attr:`~method.__self__` will actually be the class | ||
| 690 | 712 | itself, so that calling either ``x.f(1)`` or ``C.f(1)`` is equivalent to | |
| 691 | 713 | calling ``f(C,1)`` where ``f`` is the underlying function. | |
| 692 | 714 | ||
| 693 | - Note that the transformation from function object to instance method | ||
| 715 | + Note that the transformation from :ref:`function object <user-defined-funcs>` | ||
| 716 | + to instance method | ||
| 694 | 717 | object happens each time the attribute is retrieved from the instance. In | |
| 695 | 718 | some cases, a fruitful optimization is to assign the attribute to a local | |
| 696 | 719 | variable and call that local variable. Also notice that this | |
@@ -774,6 +797,8 @@ set to ``None`` (but see the next item); :attr:`__module__` is the name of | |||
| 774 | 797 | the module the function was defined in or ``None`` if unavailable. | |
| 775 | 798 | ||
| 776 | 799 | ||
| 800 | + .. _builtin-methods: | ||
| 801 | + | ||
| 777 | 802 | Built-in methods | |
| 778 | 803 | ^^^^^^^^^^^^^^^^ | |
| 779 | 804 | ||
@@ -785,8 +810,9 @@ Built-in methods | |||
| 785 | 810 | This is really a different disguise of a built-in function, this time containing | |
| 786 | 811 | an object passed to the C function as an implicit extra argument. An example of | |
| 787 | 812 | a built-in method is ``alist.append()``, assuming *alist* is a list object. In | |
| 788 | - this case, the special read-only attribute :attr:`__self__` is set to the object | ||
| 789 | - denoted by *alist*. | ||
| 813 | + this case, the special read-only attribute :attr:`!__self__` is set to the object | ||
| 814 | + denoted by *alist*. (The attribute has the same semantics as it does with | ||
| 815 | + :attr:`other instance methods <method.__self__>`.) | ||
| 790 | 816 | ||
| 791 | 817 | ||
| 792 | 818 | Classes | |
@@ -901,8 +927,9 @@ https://www.python.org/download/releases/2.3/mro/. | |||
| 901 | 927 | ||
| 902 | 928 | When a class attribute reference (for class :class:`!C`, say) would yield a | |
| 903 | 929 | class method object, it is transformed into an instance method object whose | |
| 904 | - :attr:`__self__` attribute is :class:`!C`. When it would yield a static | ||
| 905 | - method object, it is transformed into the object wrapped by the static method | ||
| 930 | + :attr:`~method.__self__` attribute is :class:`!C`. | ||
| 931 | + When it would yield a :class:`staticmethod` object, | ||
| 932 | + it is transformed into the object wrapped by the static method | ||
| 906 | 933 | object. See section :ref:`descriptors` for another way in which attributes | |
| 907 | 934 | retrieved from a class may differ from those actually contained in its | |
| 908 | 935 | :attr:`~object.__dict__`. | |
@@ -970,7 +997,7 @@ in which attribute references are searched. When an attribute is not found | |||
| 970 | 997 | there, and the instance's class has an attribute by that name, the search | |
| 971 | 998 | continues with the class attributes. If a class attribute is found that is a | |
| 972 | 999 | user-defined function object, it is transformed into an instance method | |
| 973 | - object whose :attr:`__self__` attribute is the instance. Static method and | ||
| 1000 | + object whose :attr:`~method.__self__` attribute is the instance. Static method and | ||
| 974 | 1001 | class method objects are also transformed; see above under "Classes". See | |
| 975 | 1002 | section :ref:`descriptors` for another way in which attributes of a class | |
| 976 | 1003 | retrieved via its instances may differ from the objects actually stored in | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -769,8 +769,10 @@ data from a string buffer instead, and pass it as an argument. | |||
| 769 | 769 | or arithmetic operators, and assigning such a "pseudo-file" to sys.stdin will | |
| 770 | 770 | not cause the interpreter to read further input from it.) | |
| 771 | 771 | ||
| 772 | - Instance method objects have attributes, too: ``m.__self__`` is the instance | ||
| 773 | - object with the method :meth:`!m`, and ``m.__func__`` is the function object | ||
| 772 | + :ref:`Instance method objects <instance-methods>` have attributes, too: | ||
| 773 | + :attr:`m.__self__ <method.__self__>` is the instance | ||
| 774 | + object with the method :meth:`!m`, and :attr:`m.__func__ <method.__func__>` is | ||
| 775 | + the :ref:`function object <user-defined-funcs>` | ||
| 774 | 776 | corresponding to the method. | |
| 775 | 777 | ||
| 776 | 778 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1678,8 +1678,8 @@ Some smaller changes made to the core Python language are: | |||
| 1678 | 1678 | ||
| 1679 | 1679 | * Instance method objects have new attributes for the object and function | |
| 1680 | 1680 | comprising the method; the new synonym for :attr:`!im_self` is | |
| 1681 | - :ref:`__self__ <instance-methods>`, and :attr:`!im_func` is also available as | ||
| 1682 | - :ref:`__func__ <instance-methods>`. | ||
| 1681 | + :attr:`~method.__self__`, and :attr:`!im_func` is also available as | ||
| 1682 | + :attr:`~method.__func__`. | ||
| 1683 | 1683 | The old names are still supported in Python 2.6, but are gone in 3.0. | |
| 1684 | 1684 | ||
| 1685 | 1685 | * An obscure change: when you use the :func:`locals` function inside a | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -858,9 +858,10 @@ Some smaller changes made to the core Python language are: | |||
| 858 | 858 | ||
| 859 | 859 | .. XXX bytearray doesn't seem to be documented | |
| 860 | 860 | ||
| 861 | - * When using ``@classmethod`` and ``@staticmethod`` to wrap | ||
| 861 | + * When using :class:`@classmethod <classmethod>` and | ||
| 862 | + :class:`@staticmethod <staticmethod>` to wrap | ||
| 862 | 863 | methods as class or static methods, the wrapper object now | |
| 863 | - exposes the wrapped function as their :ref:`__func__ <instance-methods>` | ||
| 864 | + exposes the wrapped function as their :attr:`~method.__func__` | ||
| 864 | 865 | attribute. | |
| 865 | 866 | (Contributed by Amaury Forgeot d'Arc, after a suggestion by | |
| 866 | 867 | George Sakkis; :issue:`5982`.) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments