@@ -534,99 +534,108 @@ section :ref:`function`). It should be called with an argument list
534
534
containing the same number of items as the function's formal parameter
535
535
list.
536
536
537
- Special attributes:
537
+ Special read-only attributes
538
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
539
+
540
+ .. index ::
541
+ single: __closure__ (function attribute)
542
+ single: __globals__ (function attribute)
543
+ pair: global; namespace
544
+
545
+ .. list-table ::
546
+ :header-rows: 1
538
547
539
- .. tabularcolumns :: |l|L|l|
548
+ * - Attribute
549
+ - Meaning
550
+
551
+ * - .. attribute:: function.__globals__
552
+ - A reference to the :class: `dictionary <dict> ` that holds the function's
553
+ :ref: `global variables <naming >` -- the global namespace of the module
554
+ in which the function was defined.
555
+
556
+ * - .. attribute:: function.__closure__
557
+ - ``None `` or a :class: `tuple ` of cells that contain bindings for the
558
+ function's free variables.
559
+
560
+ A cell object has the attribute ``cell_contents ``.
561
+ This can be used to get the value of the cell, as well as set the value.
562
+
563
+ Special writable attributes
564
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
540
565
541
566
.. index ::
542
567
single: __doc__ (function attribute)
543
568
single: __name__ (function attribute)
544
569
single: __module__ (function attribute)
545
570
single: __dict__ (function attribute)
546
571
single: __defaults__ (function attribute)
547
- single: __closure__ (function attribute)
548
572
single: __code__ (function attribute)
549
- single: __globals__ (function attribute)
550
573
single: __annotations__ (function attribute)
551
574
single: __kwdefaults__ (function attribute)
552
- pair: global; namespace
553
575
554
- +-------------------------+-------------------------------+-----------+
555
- | Attribute | Meaning | |
556
- +=========================+===============================+===========+
557
- | :attr: `__doc__ ` | The function's documentation | Writable |
558
- | | string, or ``None `` if | |
559
- | | unavailable; not inherited by | |
560
- | | subclasses. | |
561
- +-------------------------+-------------------------------+-----------+
562
- | :attr: `~definition.\ | The function's name. | Writable |
563
- | __name__` | | |
564
- +-------------------------+-------------------------------+-----------+
565
- | :attr: `~definition.\ | The function's | Writable |
566
- | __qualname__` | :term: `qualified name `. | |
567
- | | | |
568
- | | .. versionadded:: 3.3 | |
569
- +-------------------------+-------------------------------+-----------+
570
- | :attr: `__module__ ` | The name of the module the | Writable |
571
- | | function was defined in, or | |
572
- | | ``None `` if unavailable. | |
573
- +-------------------------+-------------------------------+-----------+
574
- | :attr: `__defaults__ ` | A tuple containing default | Writable |
575
- | | argument values for those | |
576
- | | arguments that have defaults, | |
577
- | | or ``None `` if no arguments | |
578
- | | have a default value. | |
579
- +-------------------------+-------------------------------+-----------+
580
- | :attr: `__code__ ` | The code object representing | Writable |
581
- | | the compiled function body. | |
582
- +-------------------------+-------------------------------+-----------+
583
- | :attr: `__globals__ ` | A reference to the dictionary | Read-only |
584
- | | that holds the function's | |
585
- | | global variables --- the | |
586
- | | global namespace of the | |
587
- | | module in which the function | |
588
- | | was defined. | |
589
- +-------------------------+-------------------------------+-----------+
590
- | :attr: `~object.__dict__`| The namespace supporting | Writable |
591
- | | arbitrary function | |
592
- | | attributes. | |
593
- +-------------------------+-------------------------------+-----------+
594
- | :attr: `__closure__ ` | ``None `` or a tuple of cells | Read-only |
595
- | | that contain bindings for the | |
596
- | | function's free variables. | |
597
- | | See below for information on | |
598
- | | the ``cell_contents `` | |
599
- | | attribute. | |
600
- +-------------------------+-------------------------------+-----------+
601
- | :attr: `__annotations__ ` | A dict containing annotations | Writable |
602
- | | of parameters. The keys of | |
603
- | | the dict are the parameter | |
604
- | | names, and ``'return' `` for | |
605
- | | the return annotation, if | |
606
- | | provided. For more | |
607
- | | information on working with | |
608
- | | this attribute, see | |
609
- | | :ref: `annotations-howto `. | |
610
- +-------------------------+-------------------------------+-----------+
611
- | :attr: `__kwdefaults__ ` | A dict containing defaults | Writable |
612
- | | for keyword-only parameters. | |
613
- +-------------------------+-------------------------------+-----------+
614
-
615
- Most of the attributes labelled "Writable" check the type of the assigned value.
576
+ Most of these attributes check the type of the assigned value:
577
+
578
+ .. list-table ::
579
+ :header-rows: 1
580
+
581
+ * - Attribute
582
+ - Meaning
583
+
584
+ * - .. attribute:: function.__doc__
585
+ - The function's documentation string, or ``None `` if unavailable.
586
+ Not inherited by subclasses.
587
+
588
+ * - .. attribute:: function.__name__
589
+ - The function's name.
590
+ See also: :attr: `__name__ attributes <definition.__name__> `.
591
+
592
+ * - .. attribute:: function.__qualname__
593
+ - The function's :term: `qualified name `.
594
+ See also: :attr: `__qualname__ attributes <definition.__qualname__> `.
595
+
596
+ .. versionadded :: 3.3
597
+
598
+ * - .. attribute:: function.__module__
599
+ - The name of the module the function was defined in,
600
+ or ``None `` if unavailable.
601
+
602
+ * - .. attribute:: function.__defaults__
603
+ - A :class: `tuple ` containing default parameter values
604
+ for those parameters that have defaults,
605
+ or ``None `` if no parameters have a default value.
606
+
607
+ * - .. attribute:: function.__code__
608
+ - The :ref: `code object <code-objects >` representing
609
+ the compiled function body.
610
+
611
+ * - .. attribute:: function.__dict__
612
+ - The namespace supporting arbitrary function attributes.
613
+ See also: :attr: `__dict__ attributes <object.__dict__> `.
614
+
615
+ * - .. attribute:: function.__annotations__
616
+ - A :class: `dictionary <dict> ` containing annotations of parameters.
617
+ The keys of the dictionary are the parameter names,
618
+ and ``'return' `` for the return annotation, if provided.
619
+ See also: :ref: `annotations-howto `.
620
+
621
+ * - .. attribute:: function.__kwdefaults__
622
+ - A :class: `dictionary <dict> ` containing defaults for keyword-only
623
+ parameters.
616
624
617
625
Function objects also support getting and setting arbitrary attributes, which
618
626
can be used, for example, to attach metadata to functions. Regular attribute
619
- dot-notation is used to get and set such attributes. *Note that the current
620
- implementation only supports function attributes on user-defined functions.
621
- Function attributes on built-in functions may be supported in the future. *
627
+ dot-notation is used to get and set such attributes.
622
628
623
- A cell object has the attribute ``cell_contents ``. This can be used to get
624
- the value of the cell, as well as set the value.
629
+ .. impl-detail ::
630
+
631
+ CPython's current implementation only supports function attributes
632
+ on user-defined functions. Function attributes on
633
+ :ref: `built-in functions <builtin-functions >` may be supported in the
634
+ future.
625
635
626
636
Additional information about a function's definition can be retrieved from its
627
- code object; see the description of internal types below. The
628
- :data: `cell <types.CellType> ` type can be accessed in the :mod: `types `
629
- module.
637
+ :ref: `code object <code-objects >`
638
+ (accessible via the :attr: `~function.__code__ ` attribute).
630
639
631
640
632
641
.. _instance-methods :
@@ -658,15 +667,17 @@ Special read-only attributes:
658
667
:ref: `bound <method-binding >`
659
668
660
669
* - .. attribute:: method.__func__
661
- - Refers to the original function object
670
+ - Refers to the original :ref: ` function object < user-defined-funcs >`
662
671
663
672
* - .. attribute:: method.__doc__
664
- - The method's documentation (same as :attr: `!method.__func__.__doc__ `).
673
+ - The method's documentation
674
+ (same as :attr: `method.__func__.__doc__ <function.__doc__> `).
665
675
A :class: `string <str> ` if the original function had a docstring, else
666
676
``None ``.
667
677
668
678
* - .. attribute:: method.__name__
669
- - The name of the method (same as :attr: `!method.__func__.__name__ `)
679
+ - The name of the method
680
+ (same as :attr: `method.__func__.__name__ <function.__name__> `)
670
681
671
682
* - .. attribute:: method.__module__
672
683
- The name of the module the method was defined in, or ``None `` if
@@ -772,6 +783,8 @@ is raised and the asynchronous iterator will have reached the end of
772
783
the set of values to be yielded.
773
784
774
785
786
+ .. _builtin-functions :
787
+
775
788
Built-in functions
776
789
^^^^^^^^^^^^^^^^^^
777
790
@@ -784,10 +797,14 @@ A built-in function object is a wrapper around a C function. Examples of
784
797
built-in functions are :func: `len ` and :func: `math.sin ` (:mod: `math ` is a
785
798
standard built-in module). The number and type of the arguments are
786
799
determined by the C function. Special read-only attributes:
787
- :attr: `__doc__ ` is the function's documentation string, or ``None `` if
788
- unavailable; :attr: `~definition.__name__ ` is the function's name; :attr: `__self__ ` is
789
- set to ``None `` (but see the next item); :attr: `__module__ ` is the name of
790
- the module the function was defined in or ``None `` if unavailable.
800
+
801
+ * :attr: `!__doc__ ` is the function's documentation string, or ``None `` if
802
+ unavailable. See :attr: `function.__doc__ `.
803
+ * :attr: `!__name__ ` is the function's name. See :attr: `function.__name__ `.
804
+ * :attr: `!__self__ ` is set to ``None `` (but see the next item).
805
+ * :attr: `!__module__ ` is the name of
806
+ the module the function was defined in or ``None `` if unavailable.
807
+ See :attr: `function.__module__ `.
791
808
792
809
793
810
.. _builtin-methods :
@@ -837,7 +854,8 @@ the :ref:`import system <importsystem>` as invoked either by the
837
854
:keyword: `import ` statement, or by calling
838
855
functions such as :func: `importlib.import_module ` and built-in
839
856
:func: `__import__ `. A module object has a namespace implemented by a
840
- dictionary object (this is the dictionary referenced by the ``__globals__ ``
857
+ :class: `dictionary <dict> ` object (this is the dictionary referenced by the
858
+ :attr: `~function.__globals__ `
841
859
attribute of functions defined in the module). Attribute references are
842
860
translated to lookups in this dictionary, e.g., ``m.x `` is equivalent to
843
861
``m.__dict__["x"] ``. A module object does not contain the code object used
@@ -1869,7 +1887,8 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
1869
1887
.. note ::
1870
1888
1871
1889
This method may still be bypassed when looking up special methods as the
1872
- result of implicit invocation via language syntax or built-in functions.
1890
+ result of implicit invocation via language syntax or
1891
+ :ref: `built-in functions <builtin-functions >`.
1873
1892
See :ref: `special-lookup `.
1874
1893
1875
1894
.. audit-event :: object.__getattr__ obj,name object.__getattribute__
0 commit comments