mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +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
|
@ -102,9 +102,9 @@ Your code will have to have a separate code path if the object
|
|||
you're examining is a class (``isinstance(o, type)``).
|
||||
In that case, best practice relies on an implementation detail
|
||||
of Python 3.9 and before: if a class has annotations defined,
|
||||
they are stored in the class's ``__dict__`` dictionary. Since
|
||||
they are stored in the class's :attr:`~type.__dict__` dictionary. Since
|
||||
the class may or may not have annotations defined, best practice
|
||||
is to call the ``get`` method on the class dict.
|
||||
is to call the :meth:`~dict.get` method on the class dict.
|
||||
|
||||
To put it all together, here is some sample code that safely
|
||||
accesses the ``__annotations__`` attribute on an arbitrary
|
||||
|
@ -121,8 +121,8 @@ the type of ``ann`` using :func:`isinstance` before further
|
|||
examination.
|
||||
|
||||
Note that some exotic or malformed type objects may not have
|
||||
a ``__dict__`` attribute, so for extra safety you may also wish
|
||||
to use :func:`getattr` to access ``__dict__``.
|
||||
a :attr:`~type.__dict__` attribute, so for extra safety you may also wish
|
||||
to use :func:`getattr` to access :attr:`!__dict__`.
|
||||
|
||||
|
||||
Manually Un-Stringizing Stringized Annotations
|
||||
|
|
|
@ -562,8 +562,8 @@ attribute access.
|
|||
|
||||
The expression ``obj.x`` looks up the attribute ``x`` in the chain of
|
||||
namespaces for ``obj``. If the search finds a descriptor outside of the
|
||||
instance ``__dict__``, its :meth:`__get__` method is invoked according to the
|
||||
precedence rules listed below.
|
||||
instance :attr:`~object.__dict__`, its :meth:`~object.__get__` method is
|
||||
invoked according to the precedence rules listed below.
|
||||
|
||||
The details of invocation depend on whether ``obj`` is an object, class, or
|
||||
instance of super.
|
||||
|
|
|
@ -608,7 +608,7 @@ The solution is to specify the module name explicitly as follows::
|
|||
the source, pickling will be disabled.
|
||||
|
||||
The new pickle protocol 4 also, in some circumstances, relies on
|
||||
:attr:`~definition.__qualname__` being set to the location where pickle will be able
|
||||
:attr:`~type.__qualname__` being set to the location where pickle will be able
|
||||
to find the class. For example, if the class was made available in class
|
||||
SomeData in the global scope::
|
||||
|
||||
|
|
|
@ -335,7 +335,7 @@ E is more specialized than C, even if it is in a higher level.
|
|||
|
||||
A lazy programmer can obtain the MRO directly from Python 2.2, since in
|
||||
this case it coincides with the Python 2.3 linearization. It is enough
|
||||
to invoke the .mro() method of class A:
|
||||
to invoke the :meth:`~type.mro` method of class A:
|
||||
|
||||
>>> A.mro() # doctest: +NORMALIZE_WHITESPACE
|
||||
[<class 'A'>, <class 'B'>, <class 'E'>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue