mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
[3.13] gh-101100: Add a table of class attributes to the "Custom classes" section of the data model docs (#124480) (#124556)
This commit is contained in:
parent
068e734bb5
commit
9f2e6ca199
41 changed files with 250 additions and 208 deletions
|
@ -99,7 +99,7 @@ a helper class :class:`ABC` to alternatively define ABCs through inheritance:
|
|||
that you can customize the behavior of :func:`issubclass` further without the
|
||||
need to call :meth:`register` on every class you want to consider a
|
||||
subclass of the ABC. (This class method is called from the
|
||||
:meth:`~class.__subclasscheck__` method of the ABC.)
|
||||
:meth:`~type.__subclasscheck__` method of the ABC.)
|
||||
|
||||
This method should return ``True``, ``False`` or :data:`NotImplemented`. If
|
||||
it returns ``True``, the *subclass* is considered a subclass of this ABC.
|
||||
|
@ -149,7 +149,7 @@ a helper class :class:`ABC` to alternatively define ABCs through inheritance:
|
|||
The :meth:`__subclasshook__` class method defined here says that any class
|
||||
that has an :meth:`~iterator.__iter__` method in its
|
||||
:attr:`~object.__dict__` (or in that of one of its base classes, accessed
|
||||
via the :attr:`~class.__mro__` list) is considered a ``MyIterable`` too.
|
||||
via the :attr:`~type.__mro__` list) is considered a ``MyIterable`` too.
|
||||
|
||||
Finally, the last line makes ``Foo`` a virtual subclass of ``MyIterable``,
|
||||
even though it does not define an :meth:`~iterator.__iter__` method (it uses
|
||||
|
|
|
@ -874,8 +874,8 @@ they add the ability to access fields by name instead of position index.
|
|||
``(1, 2)``, then ``x`` will be a required argument, ``y`` will default to
|
||||
``1``, and ``z`` will default to ``2``.
|
||||
|
||||
If *module* is defined, the ``__module__`` attribute of the named tuple is
|
||||
set to that value.
|
||||
If *module* is defined, the :attr:`~type.__module__` attribute of the
|
||||
named tuple is set to that value.
|
||||
|
||||
Named tuple instances do not have per-instance dictionaries, so they are
|
||||
lightweight and require no more memory than regular tuples.
|
||||
|
|
|
@ -58,11 +58,12 @@
|
|||
* the type itself (``typ``)
|
||||
* the type's fully qualified name (``typ.__module__ + '.' +
|
||||
typ.__qualname__``).
|
||||
* the type's qualname (``typ.__qualname__``)
|
||||
* the type's name (``typ.__name__``).
|
||||
* the type's :attr:`qualname <type.__qualname__>` (``typ.__qualname__``)
|
||||
* the type's :attr:`name <type.__name__>` (``typ.__name__``).
|
||||
|
||||
If none of the above match, repeat all of the checks above for each of
|
||||
the types in the :term:`MRO` (``typ.__mro__``). Finally, if no other key
|
||||
the types in the :term:`MRO` (:attr:`typ.__mro__ <type.__mro__>`).
|
||||
Finally, if no other key
|
||||
yields a handler, check for a handler for the key ``None``. If there is
|
||||
no handler for ``None``, raise a :exc:`KeyError` for the fully
|
||||
qualified name of the type.
|
||||
|
|
|
@ -317,7 +317,7 @@ variant, :attr:`~.BaseHeader.max_count` is set to 1.
|
|||
class. When *use_default_map* is ``True`` (the default), the standard
|
||||
mapping of header names to classes is copied in to the registry during
|
||||
initialization. *base_class* is always the last class in the generated
|
||||
class's ``__bases__`` list.
|
||||
class's :class:`~type.__bases__` list.
|
||||
|
||||
The default mappings are:
|
||||
|
||||
|
|
|
@ -283,9 +283,11 @@ are always available. They are listed here in alphabetical order.
|
|||
:func:`property`.
|
||||
|
||||
.. versionchanged:: 3.10
|
||||
Class methods now inherit the method attributes (``__module__``,
|
||||
``__name__``, ``__qualname__``, ``__doc__`` and ``__annotations__``) and
|
||||
have a new ``__wrapped__`` attribute.
|
||||
Class methods now inherit the method attributes
|
||||
(:attr:`~function.__module__`, :attr:`~function.__name__`,
|
||||
:attr:`~function.__qualname__`, :attr:`~function.__doc__` and
|
||||
:attr:`~function.__annotations__`) and have a new ``__wrapped__``
|
||||
attribute.
|
||||
|
||||
.. deprecated-removed:: 3.11 3.13
|
||||
Class methods can no longer wrap other :term:`descriptors <descriptor>` such as
|
||||
|
@ -1279,8 +1281,9 @@ are always available. They are listed here in alphabetical order.
|
|||
|
||||
.. note::
|
||||
|
||||
:class:`object` does *not* have a :attr:`~object.__dict__`, so you can't
|
||||
assign arbitrary attributes to an instance of the :class:`object` class.
|
||||
:class:`object` instances do *not* have :attr:`~object.__dict__`
|
||||
attributes, so you can't assign arbitrary attributes to an instance of
|
||||
:class:`object`.
|
||||
|
||||
|
||||
.. function:: oct(x)
|
||||
|
@ -1900,10 +1903,11 @@ are always available. They are listed here in alphabetical order.
|
|||
For more information on static methods, see :ref:`types`.
|
||||
|
||||
.. versionchanged:: 3.10
|
||||
Static methods now inherit the method attributes (``__module__``,
|
||||
``__name__``, ``__qualname__``, ``__doc__`` and ``__annotations__``),
|
||||
have a new ``__wrapped__`` attribute, and are now callable as regular
|
||||
functions.
|
||||
Static methods now inherit the method attributes
|
||||
(:attr:`~function.__module__`, :attr:`~function.__name__`,
|
||||
:attr:`~function.__qualname__`, :attr:`~function.__doc__` and
|
||||
:attr:`~function.__annotations__`), have a new ``__wrapped__`` attribute,
|
||||
and are now callable as regular functions.
|
||||
|
||||
|
||||
.. index::
|
||||
|
@ -1950,11 +1954,11 @@ are always available. They are listed here in alphabetical order.
|
|||
to be searched. The search starts from the class right after the
|
||||
*type*.
|
||||
|
||||
For example, if :attr:`~class.__mro__` of *object_or_type* is
|
||||
For example, if :attr:`~type.__mro__` of *object_or_type* is
|
||||
``D -> B -> C -> A -> object`` and the value of *type* is ``B``,
|
||||
then :func:`super` searches ``C -> A -> object``.
|
||||
|
||||
The :attr:`~class.__mro__` attribute of the class corresponding to
|
||||
The :attr:`~type.__mro__` attribute of the class corresponding to
|
||||
*object_or_type* lists the method resolution search order used by both
|
||||
:func:`getattr` and :func:`super`. The attribute is dynamic and can change
|
||||
whenever the inheritance hierarchy is updated.
|
||||
|
@ -2033,28 +2037,30 @@ are always available. They are listed here in alphabetical order.
|
|||
|
||||
With one argument, return the type of an *object*. The return value is a
|
||||
type object and generally the same object as returned by
|
||||
:attr:`object.__class__ <instance.__class__>`.
|
||||
:attr:`object.__class__`.
|
||||
|
||||
The :func:`isinstance` built-in function is recommended for testing the type
|
||||
of an object, because it takes subclasses into account.
|
||||
|
||||
|
||||
With three arguments, return a new type object. This is essentially a
|
||||
dynamic form of the :keyword:`class` statement. The *name* string is
|
||||
the class name and becomes the :attr:`~definition.__name__` attribute.
|
||||
the class name and becomes the :attr:`~type.__name__` attribute.
|
||||
The *bases* tuple contains the base classes and becomes the
|
||||
:attr:`~class.__bases__` attribute; if empty, :class:`object`, the
|
||||
:attr:`~type.__bases__` attribute; if empty, :class:`object`, the
|
||||
ultimate base of all classes, is added. The *dict* dictionary contains
|
||||
attribute and method definitions for the class body; it may be copied
|
||||
or wrapped before becoming the :attr:`~object.__dict__` attribute.
|
||||
The following two statements create identical :class:`type` objects:
|
||||
or wrapped before becoming the :attr:`~type.__dict__` attribute.
|
||||
The following two statements create identical :class:`!type` objects:
|
||||
|
||||
>>> class X:
|
||||
... a = 1
|
||||
...
|
||||
>>> X = type('X', (), dict(a=1))
|
||||
|
||||
See also :ref:`bltin-type-objects`.
|
||||
See also:
|
||||
|
||||
* :ref:`Documentation on attributes and methods on classes <class-attrs-and-methods>`.
|
||||
* :ref:`bltin-type-objects`
|
||||
|
||||
Keyword arguments provided to the three argument form are passed to the
|
||||
appropriate metaclass machinery (usually :meth:`~object.__init_subclass__`)
|
||||
|
@ -2064,18 +2070,18 @@ are always available. They are listed here in alphabetical order.
|
|||
See also :ref:`class-customization`.
|
||||
|
||||
.. versionchanged:: 3.6
|
||||
Subclasses of :class:`type` which don't override ``type.__new__`` may no
|
||||
Subclasses of :class:`!type` which don't override ``type.__new__`` may no
|
||||
longer use the one-argument form to get the type of an object.
|
||||
|
||||
.. function:: vars()
|
||||
vars(object)
|
||||
|
||||
Return the :attr:`~object.__dict__` attribute for a module, class, instance,
|
||||
or any other object with a :attr:`~object.__dict__` attribute.
|
||||
or any other object with a :attr:`!__dict__` attribute.
|
||||
|
||||
Objects such as modules and instances have an updateable :attr:`~object.__dict__`
|
||||
attribute; however, other objects may have write restrictions on their
|
||||
:attr:`~object.__dict__` attributes (for example, classes use a
|
||||
:attr:`!__dict__` attributes (for example, classes use a
|
||||
:class:`types.MappingProxyType` to prevent direct dictionary updates).
|
||||
|
||||
Without an argument, :func:`vars` acts like :func:`locals`.
|
||||
|
|
|
@ -646,10 +646,11 @@ The :mod:`functools` module defines the following functions:
|
|||
attributes of the wrapper function are updated with the corresponding attributes
|
||||
from the original function. The default values for these arguments are the
|
||||
module level constants ``WRAPPER_ASSIGNMENTS`` (which assigns to the wrapper
|
||||
function's ``__module__``, ``__name__``, ``__qualname__``, ``__annotations__``,
|
||||
``__type_params__``, and ``__doc__``, the documentation string)
|
||||
and ``WRAPPER_UPDATES`` (which
|
||||
updates the wrapper function's ``__dict__``, i.e. the instance dictionary).
|
||||
function's :attr:`~function.__module__`, :attr:`~function.__name__`,
|
||||
:attr:`~function.__qualname__`, :attr:`~function.__annotations__`,
|
||||
:attr:`~function.__type_params__`, and :attr:`~function.__doc__`, the
|
||||
documentation string) and ``WRAPPER_UPDATES`` (which updates the wrapper
|
||||
function's :attr:`~function.__dict__`, i.e. the instance dictionary).
|
||||
|
||||
To allow access to the original function for introspection and other purposes
|
||||
(e.g. bypassing a caching decorator such as :func:`lru_cache`), this function
|
||||
|
@ -670,7 +671,7 @@ The :mod:`functools` module defines the following functions:
|
|||
|
||||
.. versionchanged:: 3.2
|
||||
The ``__wrapped__`` attribute is now automatically added.
|
||||
The ``__annotations__`` attribute is now copied by default.
|
||||
The :attr:`~function.__annotations__` attribute is now copied by default.
|
||||
Missing attributes no longer trigger an :exc:`AttributeError`.
|
||||
|
||||
.. versionchanged:: 3.4
|
||||
|
@ -679,7 +680,7 @@ The :mod:`functools` module defines the following functions:
|
|||
(see :issue:`17482`)
|
||||
|
||||
.. versionchanged:: 3.12
|
||||
The ``__type_params__`` attribute is now copied by default.
|
||||
The :attr:`~function.__type_params__` attribute is now copied by default.
|
||||
|
||||
|
||||
.. decorator:: wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
|
||||
|
@ -741,9 +742,10 @@ have three read-only attributes:
|
|||
The keyword arguments that will be supplied when the :class:`partial` object is
|
||||
called.
|
||||
|
||||
:class:`partial` objects are like :class:`function` objects in that they are
|
||||
callable, weak referenceable, and can have attributes. There are some important
|
||||
differences. For instance, the :attr:`~definition.__name__` and :attr:`__doc__` attributes
|
||||
:class:`partial` objects are like :ref:`function objects <user-defined-funcs>`
|
||||
in that they are callable, weak referenceable, and can have attributes.
|
||||
There are some important differences. For instance, the
|
||||
:attr:`~function.__name__` and :attr:`function.__doc__` attributes
|
||||
are not created automatically. Also, :class:`partial` objects defined in
|
||||
classes behave like static methods and do not transform into bound methods
|
||||
during instance attribute look-up.
|
||||
|
|
|
@ -520,7 +520,7 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
|
|||
has a :meth:`~object.__get__` method, but not a :meth:`~object.__set__`
|
||||
method or a :meth:`~object.__delete__` method. Beyond that, the set of
|
||||
attributes varies. A :attr:`~definition.__name__` attribute is usually
|
||||
sensible, and :attr:`!__doc__` often is.
|
||||
sensible, and :attr:`~definition.__doc__` often is.
|
||||
|
||||
Methods implemented via descriptors that also pass one of the other tests
|
||||
return ``False`` from the :func:`ismethoddescriptor` test, simply because the
|
||||
|
|
|
@ -304,7 +304,8 @@ in a module, ``__name__`` is the module's name in the Python package namespace.
|
|||
parameter mirrors the equivalent one in the :mod:`warnings` module.
|
||||
|
||||
The fourth keyword argument is *extra* which can be used to pass a
|
||||
dictionary which is used to populate the __dict__ of the :class:`LogRecord`
|
||||
dictionary which is used to populate the :attr:`~object.__dict__` of the
|
||||
:class:`LogRecord`
|
||||
created for the logging event with user-defined attributes. These custom
|
||||
attributes can then be used as you like. For example, they could be
|
||||
incorporated into logged messages. For example::
|
||||
|
|
|
@ -21,7 +21,7 @@ modules. The documentation can be presented as pages of text on the console,
|
|||
served to a web browser, or saved to HTML files.
|
||||
|
||||
For modules, classes, functions and methods, the displayed documentation is
|
||||
derived from the docstring (i.e. the :attr:`!__doc__` attribute) of the object,
|
||||
derived from the docstring (i.e. the :attr:`~definition.__doc__` attribute) of the object,
|
||||
and recursively of its documentable members. If there is no docstring,
|
||||
:mod:`!pydoc` tries to obtain a description from the block of comment lines just
|
||||
above the definition of the class, function or method in the source file, or at
|
||||
|
|
|
@ -5486,22 +5486,6 @@ types, where they are relevant. Some of these are not reported by the
|
|||
:func:`dir` built-in function.
|
||||
|
||||
|
||||
.. attribute:: object.__dict__
|
||||
|
||||
A dictionary or other mapping object used to store an object's (writable)
|
||||
attributes.
|
||||
|
||||
|
||||
.. attribute:: instance.__class__
|
||||
|
||||
The class to which a class instance belongs.
|
||||
|
||||
|
||||
.. attribute:: class.__bases__
|
||||
|
||||
The tuple of base classes of a class object.
|
||||
|
||||
|
||||
.. attribute:: definition.__name__
|
||||
|
||||
The name of the class, function, method, descriptor, or
|
||||
|
@ -5516,44 +5500,25 @@ types, where they are relevant. Some of these are not reported by the
|
|||
.. versionadded:: 3.3
|
||||
|
||||
|
||||
.. attribute:: definition.__module__
|
||||
|
||||
The name of the module in which a class or function was defined.
|
||||
|
||||
|
||||
.. attribute:: definition.__doc__
|
||||
|
||||
The documentation string of a class or function, or ``None`` if undefined.
|
||||
|
||||
|
||||
.. attribute:: definition.__type_params__
|
||||
|
||||
The :ref:`type parameters <type-params>` of generic classes, functions,
|
||||
and :ref:`type aliases <type-aliases>`.
|
||||
and :ref:`type aliases <type-aliases>`. For classes and functions that
|
||||
are not generic, this will be an empty tuple.
|
||||
|
||||
.. versionadded:: 3.12
|
||||
|
||||
|
||||
.. attribute:: class.__mro__
|
||||
|
||||
This attribute is a tuple of classes that are considered when looking for
|
||||
base classes during method resolution.
|
||||
|
||||
|
||||
.. method:: class.mro()
|
||||
|
||||
This method can be overridden by a metaclass to customize the method
|
||||
resolution order for its instances. It is called at class instantiation, and
|
||||
its result is stored in :attr:`~class.__mro__`.
|
||||
|
||||
|
||||
.. method:: class.__subclasses__
|
||||
|
||||
Each class keeps a list of weak references to its immediate subclasses. This
|
||||
method returns a list of all those references still alive. The list is in
|
||||
definition order. Example::
|
||||
|
||||
>>> int.__subclasses__()
|
||||
[<class 'bool'>, <enum 'IntEnum'>, <flag 'IntFlag'>, <class 're._constants._NamedIntConstant'>]
|
||||
|
||||
|
||||
.. attribute:: class.__static_attributes__
|
||||
|
||||
A tuple containing names of attributes of this class which are accessed
|
||||
through ``self.X`` from any function in its body.
|
||||
|
||||
.. versionadded:: 3.13
|
||||
|
||||
.. _int_max_str_digits:
|
||||
|
||||
Integer string conversion length limitation
|
||||
|
|
|
@ -946,7 +946,7 @@ The :mod:`test.support` module defines the following functions:
|
|||
other modules, possibly a C backend (like ``csv`` and its ``_csv``).
|
||||
|
||||
The *extra* argument can be a set of names that wouldn't otherwise be automatically
|
||||
detected as "public", like objects without a proper ``__module__``
|
||||
detected as "public", like objects without a proper :attr:`~definition.__module__`
|
||||
attribute. If provided, it will be added to the automatically detected ones.
|
||||
|
||||
The *not_exported* argument can be a set of names that must not be treated
|
||||
|
|
|
@ -91,8 +91,8 @@ Dynamic Type Creation
|
|||
|
||||
For classes that have an ``__orig_bases__`` attribute, this
|
||||
function returns the value of ``cls.__orig_bases__``.
|
||||
For classes without the ``__orig_bases__`` attribute, ``cls.__bases__`` is
|
||||
returned.
|
||||
For classes without the ``__orig_bases__`` attribute,
|
||||
:attr:`cls.__bases__ <type.__bases__>` is returned.
|
||||
|
||||
Examples::
|
||||
|
||||
|
@ -392,7 +392,7 @@ Standard names are defined for the following types:
|
|||
|
||||
In addition, when a class is defined with a :attr:`~object.__slots__` attribute, then for
|
||||
each slot, an instance of :class:`!MemberDescriptorType` will be added as an attribute
|
||||
on the class. This allows the slot to appear in the class's :attr:`~object.__dict__`.
|
||||
on the class. This allows the slot to appear in the class's :attr:`~type.__dict__`.
|
||||
|
||||
.. impl-detail::
|
||||
|
||||
|
|
|
@ -3193,7 +3193,8 @@ Introspection helpers
|
|||
empty dictionary is returned.
|
||||
* If *obj* is a class ``C``, the function returns a dictionary that merges
|
||||
annotations from ``C``'s base classes with those on ``C`` directly. This
|
||||
is done by traversing ``C.__mro__`` and iteratively combining
|
||||
is done by traversing :attr:`C.__mro__ <type.__mro__>` and iteratively
|
||||
combining
|
||||
``__annotations__`` dictionaries. Annotations on classes appearing
|
||||
earlier in the :term:`method resolution order` always take precedence over
|
||||
annotations on classes appearing later in the method resolution order.
|
||||
|
|
|
@ -239,7 +239,7 @@ the *new_callable* argument to :func:`patch`.
|
|||
Accessing any attribute not in this list will raise an :exc:`AttributeError`.
|
||||
|
||||
If *spec* is an object (rather than a list of strings) then
|
||||
:attr:`~instance.__class__` returns the class of the spec object. This
|
||||
:attr:`~object.__class__` returns the class of the spec object. This
|
||||
allows mocks to pass :func:`isinstance` tests.
|
||||
|
||||
* *spec_set*: A stricter variant of *spec*. If used, attempting to *set*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue