Skip to content

Commit 25d9b26

Browse files
author
Yuki Kobayashi
committed
Fixup: Fix references of objects dunders in howto/*
1 parent da545de commit 25d9b26

File tree

3 files changed

+66
-67
lines changed

3 files changed

+66
-67
lines changed

Doc/howto/descriptor.rst

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ add new capabilities one by one.
4242
Simple example: A descriptor that returns a constant
4343
----------------------------------------------------
4444

45-
The :class:`!Ten` class is a descriptor whose :meth:`__get__` method always
45+
The :class:`!Ten` class is a descriptor whose :meth:`~object.__get__` method always
4646
returns the constant ``10``:
4747

4848
.. testcode::
@@ -120,10 +120,10 @@ different, updated answers each time::
120120
2
121121

122122
Besides showing how descriptors can run computations, this example also
123-
reveals the purpose of the parameters to :meth:`__get__`. The *self*
123+
reveals the purpose of the parameters to :meth:`~object.__get__`. The *self*
124124
parameter is *size*, an instance of *DirectorySize*. The *obj* parameter is
125125
either *g* or *s*, an instance of *Directory*. It is the *obj* parameter that
126-
lets the :meth:`__get__` method learn the target directory. The *objtype*
126+
lets the :meth:`~object.__get__` method learn the target directory. The *objtype*
127127
parameter is the class *Directory*.
128128

129129

@@ -133,7 +133,7 @@ Managed attributes
133133
A popular use for descriptors is managing access to instance data. The
134134
descriptor is assigned to a public attribute in the class dictionary while the
135135
actual data is stored as a private attribute in the instance dictionary. The
136-
descriptor's :meth:`__get__` and :meth:`__set__` methods are triggered when
136+
descriptor's :meth:`~object.__get__` and :meth:`~object.__set__` methods are triggered when
137137
the public attribute is accessed.
138138

139139
In the following example, *age* is the public attribute and *_age* is the
@@ -217,7 +217,7 @@ variable name was used.
217217

218218
In this example, the :class:`!Person` class has two descriptor instances,
219219
*name* and *age*. When the :class:`!Person` class is defined, it makes a
220-
callback to :meth:`__set_name__` in *LoggedAccess* so that the field names can
220+
callback to :meth:`~object.__set_name__` in *LoggedAccess* so that the field names can
221221
be recorded, giving each descriptor its own *public_name* and *private_name*:
222222

223223
.. testcode::
@@ -254,7 +254,7 @@ be recorded, giving each descriptor its own *public_name* and *private_name*:
254254
self.age += 1
255255

256256
An interactive session shows that the :class:`!Person` class has called
257-
:meth:`__set_name__` so that the field names would be recorded. Here
257+
:meth:`~object.__set_name__` so that the field names would be recorded. Here
258258
we call :func:`vars` to look up the descriptor without triggering it:
259259

260260
.. doctest::
@@ -294,10 +294,10 @@ The two *Person* instances contain only the private names:
294294
Closing thoughts
295295
----------------
296296

297-
A :term:`descriptor` is what we call any object that defines :meth:`__get__`,
298-
:meth:`__set__`, or :meth:`__delete__`.
297+
A :term:`descriptor` is what we call any object that defines :meth:`~object.__get__`,
298+
:meth:`~object.__set__`, or :meth:`~object.__delete__`.
299299

300-
Optionally, descriptors can have a :meth:`__set_name__` method. This is only
300+
Optionally, descriptors can have a :meth:`~object.__set_name__` method. This is only
301301
used in cases where a descriptor needs to know either the class where it was
302302
created or the name of class variable it was assigned to. (This method, if
303303
present, is called even if the class is not a descriptor.)
@@ -501,8 +501,8 @@ Definition and introduction
501501
---------------------------
502502

503503
In general, a descriptor is an attribute value that has one of the methods in
504-
the descriptor protocol. Those methods are :meth:`__get__`, :meth:`__set__`,
505-
and :meth:`__delete__`. If any of those methods are defined for an
504+
the descriptor protocol. Those methods are :meth:`~object.__get__`, :meth:`~object.__set__`,
505+
and :meth:`~object.__delete__`. If any of those methods are defined for an
506506
attribute, it is said to be a :term:`descriptor`.
507507

508508
The default behavior for attribute access is to get, set, or delete the
@@ -534,8 +534,8 @@ That is all there is to it. Define any of these methods and an object is
534534
considered a descriptor and can override default behavior upon being looked up
535535
as an attribute.
536536

537-
If an object defines :meth:`__set__` or :meth:`__delete__`, it is considered
538-
a data descriptor. Descriptors that only define :meth:`__get__` are called
537+
If an object defines :meth:`~object.__set__` or :meth:`~object.__delete__`, it is considered
538+
a data descriptor. Descriptors that only define :meth:`~object.__get__` are called
539539
non-data descriptors (they are often used for methods but other uses are
540540
possible).
541541

@@ -545,9 +545,9 @@ has an entry with the same name as a data descriptor, the data descriptor
545545
takes precedence. If an instance's dictionary has an entry with the same
546546
name as a non-data descriptor, the dictionary entry takes precedence.
547547

548-
To make a read-only data descriptor, define both :meth:`__get__` and
549-
:meth:`__set__` with the :meth:`__set__` raising an :exc:`AttributeError` when
550-
called. Defining the :meth:`__set__` method with an exception raising
548+
To make a read-only data descriptor, define both :meth:`~object.__get__` and
549+
:meth:`~object.__set__` with the :meth:`~object.__set__` raising an :exc:`AttributeError` when
550+
called. Defining the :meth:`~object.__set__` method with an exception raising
551551
placeholder is enough to make it a data descriptor.
552552

553553

@@ -574,7 +574,7 @@ Invocation from an instance
574574

575575
Instance lookup scans through a chain of namespaces giving data descriptors
576576
the highest priority, followed by instance variables, then non-data
577-
descriptors, then class variables, and lastly :meth:`__getattr__` if it is
577+
descriptors, then class variables, and lastly :meth:`~object.__getattr__` if it is
578578
provided.
579579

580580
If a descriptor is found for ``a.x``, then it is invoked with:
@@ -719,12 +719,12 @@ a pure Python equivalent:
719719
>>> object_getattribute(u2, 'x') == u2.x == (D1, u2, U2)
720720
True
721721

722-
Note, there is no :meth:`__getattr__` hook in the :meth:`__getattribute__`
723-
code. That is why calling :meth:`__getattribute__` directly or with
724-
``super().__getattribute__`` will bypass :meth:`__getattr__` entirely.
722+
Note, there is no :meth:`~object.__getattr__` hook in the :meth:`~object.__getattribute__`
723+
code. That is why calling :meth:`~object.__getattribute__` directly or with
724+
``super().__getattribute__`` will bypass :meth:`~object.__getattr__` entirely.
725725

726726
Instead, it is the dot operator and the :func:`getattr` function that are
727-
responsible for invoking :meth:`__getattr__` whenever :meth:`__getattribute__`
727+
responsible for invoking :meth:`~object.__getattr__` whenever :meth:`~object.__getattribute__`
728728
raises an :exc:`AttributeError`. Their logic is encapsulated in a helper
729729
function:
730730

@@ -776,8 +776,8 @@ Invocation from a class
776776
-----------------------
777777

778778
The logic for a dotted lookup such as ``A.x`` is in
779-
:meth:`type.__getattribute__`. The steps are similar to those for
780-
:meth:`object.__getattribute__` but the instance dictionary lookup is replaced
779+
:meth:`!type.__getattribute__`. The steps are similar to those for
780+
:meth:`!object.__getattribute__` but the instance dictionary lookup is replaced
781781
by a search through the class's :term:`method resolution order`.
782782

783783
If a descriptor is found, it is invoked with ``desc.__get__(None, A)``.
@@ -789,7 +789,7 @@ The full C implementation can be found in :c:func:`!type_getattro` and
789789
Invocation from super
790790
---------------------
791791

792-
The logic for super's dotted lookup is in the :meth:`__getattribute__` method for
792+
The logic for super's dotted lookup is in the :meth:`~object.__getattribute__` method for
793793
object returned by :func:`super`.
794794

795795
A dotted lookup such as ``super(A, obj).m`` searches ``obj.__class__.__mro__``
@@ -806,21 +806,21 @@ The full C implementation can be found in :c:func:`!super_getattro` in
806806
Summary of invocation logic
807807
---------------------------
808808

809-
The mechanism for descriptors is embedded in the :meth:`__getattribute__`
809+
The mechanism for descriptors is embedded in the :meth:`~object.__getattribute__`
810810
methods for :class:`object`, :class:`type`, and :func:`super`.
811811

812812
The important points to remember are:
813813

814-
* Descriptors are invoked by the :meth:`__getattribute__` method.
814+
* Descriptors are invoked by the :meth:`~object.__getattribute__` method.
815815

816816
* Classes inherit this machinery from :class:`object`, :class:`type`, or
817817
:func:`super`.
818818

819-
* Overriding :meth:`__getattribute__` prevents automatic descriptor calls
819+
* Overriding :meth:`~object.__getattribute__` prevents automatic descriptor calls
820820
because all the descriptor logic is in that method.
821821

822-
* :meth:`object.__getattribute__` and :meth:`type.__getattribute__` make
823-
different calls to :meth:`__get__`. The first includes the instance and may
822+
* :meth:`!object.__getattribute__` and :meth:`!type.__getattribute__` make
823+
different calls to :meth:`~object.__get__`. The first includes the instance and may
824824
include the class. The second puts in ``None`` for the instance and always
825825
includes the class.
826826

@@ -835,16 +835,16 @@ Automatic name notification
835835
Sometimes it is desirable for a descriptor to know what class variable name it
836836
was assigned to. When a new class is created, the :class:`type` metaclass
837837
scans the dictionary of the new class. If any of the entries are descriptors
838-
and if they define :meth:`__set_name__`, that method is called with two
838+
and if they define :meth:`~object.__set_name__`, that method is called with two
839839
arguments. The *owner* is the class where the descriptor is used, and the
840840
*name* is the class variable the descriptor was assigned to.
841841

842842
The implementation details are in :c:func:`!type_new` and
843843
:c:func:`!set_names` in :source:`Objects/typeobject.c`.
844844

845-
Since the update logic is in :meth:`type.__new__`, notifications only take
845+
Since the update logic is in :meth:`!type.__new__`, notifications only take
846846
place at the time of class creation. If descriptors are added to the class
847-
afterwards, :meth:`__set_name__` will need to be called manually.
847+
afterwards, :meth:`~object.__set_name__` will need to be called manually.
848848

849849

850850
ORM example
@@ -1187,7 +1187,7 @@ roughly equivalent to:
11871187
return self
11881188

11891189
To support automatic creation of methods, functions include the
1190-
:meth:`__get__` method for binding methods during attribute access. This
1190+
:meth:`~object.__get__` method for binding methods during attribute access. This
11911191
means that functions are non-data descriptors that return bound methods
11921192
during dotted lookup from an instance. Here's how it works:
11931193

@@ -1231,19 +1231,19 @@ The function has a :term:`qualified name` attribute to support introspection:
12311231
'D.f'
12321232

12331233
Accessing the function through the class dictionary does not invoke
1234-
:meth:`__get__`. Instead, it just returns the underlying function object::
1234+
:meth:`~object.__get__`. Instead, it just returns the underlying function object::
12351235

12361236
>>> D.__dict__['f']
12371237
<function D.f at 0x00C45070>
12381238

1239-
Dotted access from a class calls :meth:`__get__` which just returns the
1239+
Dotted access from a class calls :meth:`~object.__get__` which just returns the
12401240
underlying function unchanged::
12411241

12421242
>>> D.f
12431243
<function D.f at 0x00C45070>
12441244

12451245
The interesting behavior occurs during dotted access from an instance. The
1246-
dotted lookup calls :meth:`__get__` which returns a bound method object::
1246+
dotted lookup calls :meth:`~object.__get__` which returns a bound method object::
12471247

12481248
>>> d = D()
12491249
>>> d.f
@@ -1268,7 +1268,7 @@ Kinds of methods
12681268
Non-data descriptors provide a simple mechanism for variations on the usual
12691269
patterns of binding functions into methods.
12701270

1271-
To recap, functions have a :meth:`__get__` method so that they can be converted
1271+
To recap, functions have a :meth:`~object.__get__` method so that they can be converted
12721272
to a method when accessed as attributes. The non-data descriptor transforms an
12731273
``obj.f(*args)`` call into ``f(obj, *args)``. Calling ``cls.f(*args)``
12741274
becomes ``f(*args)``.
@@ -1671,7 +1671,7 @@ by member descriptors:
16711671
'Emulate member_repr() in Objects/descrobject.c'
16721672
return f'<Member {self.name!r} of {self.clsname!r}>'
16731673

1674-
The :meth:`type.__new__` method takes care of adding member objects to class
1674+
The :meth:`!type.__new__` method takes care of adding member objects to class
16751675
variables:
16761676

16771677
.. testcode::

Doc/howto/enum.rst

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -424,18 +424,18 @@ Then::
424424
The rules for what is allowed are as follows: names that start and end with
425425
a single underscore are reserved by enum and cannot be used; all other
426426
attributes defined within an enumeration will become members of this
427-
enumeration, with the exception of special methods (:meth:`__str__`,
428-
:meth:`__add__`, etc.), descriptors (methods are also descriptors), and
427+
enumeration, with the exception of special methods (:meth:`~object.__str__`,
428+
:meth:`~object.__add__`, etc.), descriptors (methods are also descriptors), and
429429
variable names listed in :attr:`_ignore_`.
430430

431-
Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__`,
431+
Note: if your enumeration defines :meth:`~object.__new__` and/or :meth:`~object.__init__`,
432432
any value(s) given to the enum member will be passed into those methods.
433433
See `Planet`_ for an example.
434434

435435
.. note::
436436

437-
The :meth:`__new__` method, if defined, is used during creation of the Enum
438-
members; it is then replaced by Enum's :meth:`__new__` which is used after
437+
The :meth:`~object.__new__` method, if defined, is used during creation of the Enum
438+
members; it is then replaced by Enum's :meth:`~object.__new__` which is used after
439439
class creation for lookup of existing members. See :ref:`new-vs-init` for
440440
more details.
441441

@@ -544,7 +544,7 @@ from that module.
544544
nested in other classes.
545545

546546
It is possible to modify how enum members are pickled/unpickled by defining
547-
:meth:`__reduce_ex__` in the enumeration class. The default method is by-value,
547+
:meth:`~object.__reduce_ex__` in the enumeration class. The default method is by-value,
548548
but enums with complicated values may want to use by-name::
549549

550550
>>> import enum
@@ -908,29 +908,29 @@ Some rules:
908908
4. When another data type is mixed in, the :attr:`value` attribute is *not the
909909
same* as the enum member itself, although it is equivalent and will compare
910910
equal.
911-
5. A ``data type`` is a mixin that defines :meth:`__new__`, or a
911+
5. A ``data type`` is a mixin that defines :meth:`~object.__new__`, or a
912912
:class:`~dataclasses.dataclass`
913913
6. %-style formatting: ``%s`` and ``%r`` call the :class:`Enum` class's
914-
:meth:`__str__` and :meth:`__repr__` respectively; other codes (such as
914+
:meth:`~object.__str__` and :meth:`~object.__repr__` respectively; other codes (such as
915915
``%i`` or ``%h`` for IntEnum) treat the enum member as its mixed-in type.
916916
7. :ref:`Formatted string literals <f-strings>`, :meth:`str.format`,
917-
and :func:`format` will use the enum's :meth:`__str__` method.
917+
and :func:`format` will use the enum's :meth:`~object.__str__` method.
918918

919919
.. note::
920920

921921
Because :class:`IntEnum`, :class:`IntFlag`, and :class:`StrEnum` are
922922
designed to be drop-in replacements for existing constants, their
923-
:meth:`__str__` method has been reset to their data types'
924-
:meth:`__str__` method.
923+
:meth:`~object.__str__` method has been reset to their data types'
924+
:meth:`~object.__str__` method.
925925

926926
.. _new-vs-init:
927927

928-
When to use :meth:`__new__` vs. :meth:`__init__`
929-
------------------------------------------------
928+
When to use :meth:`~object.__new__` vs. :meth:`~object.__init__`
929+
----------------------------------------------------------------
930930

931-
:meth:`__new__` must be used whenever you want to customize the actual value of
931+
:meth:`~object.__new__` must be used whenever you want to customize the actual value of
932932
the :class:`Enum` member. Any other modifications may go in either
933-
:meth:`__new__` or :meth:`__init__`, with :meth:`__init__` being preferred.
933+
:meth:`~object.__new__` or :meth:`~object.__init__`, with :meth:`~object.__init__` being preferred.
934934

935935
For example, if you want to pass several items to the constructor, but only
936936
want one of them to be the value::
@@ -972,7 +972,7 @@ Supported ``__dunder__`` names
972972
:attr:`__members__` is a read-only ordered mapping of ``member_name``:``member``
973973
items. It is only available on the class.
974974

975-
:meth:`__new__`, if specified, must create and return the enum members; it is
975+
:meth:`~object.__new__`, if specified, must create and return the enum members; it is
976976
also a very good idea to set the member's :attr:`_value_` appropriately. Once
977977
all the members are created it is no longer used.
978978

@@ -1216,12 +1216,12 @@ Enum Classes
12161216
^^^^^^^^^^^^
12171217

12181218
The :class:`EnumType` metaclass is responsible for providing the
1219-
:meth:`__contains__`, :meth:`__dir__`, :meth:`__iter__` and other methods that
1219+
:meth:`~object.__contains__`, :meth:`~object.__dir__`, :meth:`~object.__iter__` and other methods that
12201220
allow one to do things with an :class:`Enum` class that fail on a typical
12211221
class, such as ``list(Color)`` or ``some_enum_var in Color``. :class:`EnumType` is
12221222
responsible for ensuring that various other methods on the final :class:`Enum`
1223-
class are correct (such as :meth:`__new__`, :meth:`__getnewargs__`,
1224-
:meth:`__str__` and :meth:`__repr__`).
1223+
class are correct (such as :meth:`~object.__new__`, :meth:`~object.__getnewargs__`,
1224+
:meth:`~object.__str__` and :meth:`~object.__repr__`).
12251225

12261226
Flag Classes
12271227
^^^^^^^^^^^^
@@ -1236,7 +1236,7 @@ Enum Members (aka instances)
12361236

12371237
The most interesting thing about enum members is that they are singletons.
12381238
:class:`EnumType` creates them all while it is creating the enum class itself,
1239-
and then puts a custom :meth:`__new__` in place to ensure that no new ones are
1239+
and then puts a custom :meth:`~object.__new__` in place to ensure that no new ones are
12401240
ever instantiated by returning only the existing member instances.
12411241

12421242
Flag Members
@@ -1284,7 +1284,7 @@ is. There are several ways to define this type of simple enumeration:
12841284
- use instances of :class:`auto` for the value
12851285
- use instances of :class:`object` as the value
12861286
- use a descriptive string as the value
1287-
- use a tuple as the value and a custom :meth:`__new__` to replace the
1287+
- use a tuple as the value and a custom :meth:`~object.__new__` to replace the
12881288
tuple with an :class:`int` value
12891289

12901290
Using any of these methods signifies to the user that these values are not
@@ -1320,7 +1320,7 @@ Using :class:`object` would look like::
13201320
<Color.GREEN: <object object at 0x...>>
13211321

13221322
This is also a good example of why you might want to write your own
1323-
:meth:`__repr__`::
1323+
:meth:`~object.__repr__`::
13241324

13251325
>>> class Color(Enum):
13261326
... RED = object()
@@ -1348,10 +1348,10 @@ Using a string as the value would look like::
13481348
<Color.GREEN: 'go'>
13491349

13501350

1351-
Using a custom :meth:`__new__`
1352-
""""""""""""""""""""""""""""""
1351+
Using a custom :meth:`~object.__new__`
1352+
""""""""""""""""""""""""""""""""""""""
13531353

1354-
Using an auto-numbering :meth:`__new__` would look like::
1354+
Using an auto-numbering :meth:`~object.__new__` would look like::
13551355

13561356
>>> class AutoNumber(Enum):
13571357
... def __new__(cls):
@@ -1397,8 +1397,8 @@ to handle any extra arguments::
13971397

13981398
.. note::
13991399

1400-
The :meth:`__new__` method, if defined, is used during creation of the Enum
1401-
members; it is then replaced by Enum's :meth:`__new__` which is used after
1400+
The :meth:`~object.__new__` method, if defined, is used during creation of the Enum
1401+
members; it is then replaced by Enum's :meth:`~object.__new__` which is used after
14021402
class creation for lookup of existing members.
14031403

14041404
.. warning::
@@ -1504,7 +1504,7 @@ Supports having more than one value per member::
15041504
Planet
15051505
^^^^^^
15061506

1507-
If :meth:`__new__` or :meth:`__init__` is defined, the value of the enum member
1507+
If :meth:`~object.__new__` or :meth:`~object.__init__` is defined, the value of the enum member
15081508
will be passed to those methods::
15091509

15101510
>>> class Planet(Enum):

0 commit comments

Comments
 (0)