@@ -91,7 +91,7 @@ Attributes may be read-only or writable. In the latter case, assignment to
91
91
attributes is possible. Module attributes are writable: you can write
92
92
``modname.the_answer = 42 ``. Writable attributes may also be deleted with the
93
93
:keyword: `del ` statement. For example, ``del modname.the_answer `` will remove
94
- the attribute :attr: `the_answer ` from the object named by ``modname ``.
94
+ the attribute :attr: `! the_answer ` from the object named by ``modname ``.
95
95
96
96
Namespaces are created at different moments and have different lifetimes. The
97
97
namespace containing the built-in names is created when the Python interpreter
@@ -249,7 +249,7 @@ created. This is basically a wrapper around the contents of the namespace
249
249
created by the class definition; we'll learn more about class objects in the
250
250
next section. The original local scope (the one in effect just before the class
251
251
definition was entered) is reinstated, and the class object is bound here to the
252
- class name given in the class definition header (:class: `ClassName ` in the
252
+ class name given in the class definition header (:class: `! ClassName ` in the
253
253
example).
254
254
255
255
@@ -291,20 +291,20 @@ variable ``x``.
291
291
The instantiation operation ("calling" a class object) creates an empty object.
292
292
Many classes like to create objects with instances customized to a specific
293
293
initial state. Therefore a class may define a special method named
294
- :meth: `__init__ `, like this::
294
+ :meth: `~object. __init__ `, like this::
295
295
296
296
def __init__(self):
297
297
self.data = []
298
298
299
- When a class defines an :meth: `__init__ ` method, class instantiation
300
- automatically invokes :meth: `__init__ ` for the newly created class instance. So
299
+ When a class defines an :meth: `~object. __init__ ` method, class instantiation
300
+ automatically invokes :meth: `! __init__ ` for the newly created class instance. So
301
301
in this example, a new, initialized instance can be obtained by::
302
302
303
303
x = MyClass()
304
304
305
- Of course, the :meth: `__init__ ` method may have arguments for greater
305
+ Of course, the :meth: `~object. __init__ ` method may have arguments for greater
306
306
flexibility. In that case, arguments given to the class instantiation operator
307
- are passed on to :meth: `__init__ `. For example, ::
307
+ are passed on to :meth: `! __init__ `. For example, ::
308
308
309
309
>>> class Complex:
310
310
... def __init__(self, realpart, imagpart):
@@ -328,7 +328,7 @@ attribute names: data attributes and methods.
328
328
*data attributes * correspond to "instance variables" in Smalltalk, and to "data
329
329
members" in C++. Data attributes need not be declared; like local variables,
330
330
they spring into existence when they are first assigned to. For example, if
331
- ``x `` is the instance of :class: `MyClass ` created above, the following piece of
331
+ ``x `` is the instance of :class: `! MyClass ` created above, the following piece of
332
332
code will print the value ``16 ``, without leaving a trace::
333
333
334
334
x.counter = 1
@@ -363,7 +363,7 @@ Usually, a method is called right after it is bound::
363
363
364
364
x.f()
365
365
366
- In the :class: `MyClass ` example, this will return the string ``'hello world' ``.
366
+ In the :class: `! MyClass ` example, this will return the string ``'hello world' ``.
367
367
However, it is not necessary to call a method right away: ``x.f `` is a method
368
368
object, and can be stored away and called at a later time. For example::
369
369
@@ -375,7 +375,7 @@ will continue to print ``hello world`` until the end of time.
375
375
376
376
What exactly happens when a method is called? You may have noticed that
377
377
``x.f() `` was called without an argument above, even though the function
378
- definition for :meth: `f ` specified an argument. What happened to the argument?
378
+ definition for :meth: `! f ` specified an argument. What happened to the argument?
379
379
Surely Python raises an exception when a function that requires an argument is
380
380
called without any --- even if the argument isn't actually used...
381
381
@@ -532,9 +532,9 @@ variable in the class is also ok. For example::
532
532
533
533
h = g
534
534
535
- Now ``f ``, ``g `` and ``h `` are all attributes of class :class: `C ` that refer to
535
+ Now ``f ``, ``g `` and ``h `` are all attributes of class :class: `! C ` that refer to
536
536
function objects, and consequently they are all methods of instances of
537
- :class: `C ` --- ``h `` being exactly equivalent to ``g ``. Note that this practice
537
+ :class: `! C ` --- ``h `` being exactly equivalent to ``g ``. Note that this practice
538
538
usually only serves to confuse the reader of a program.
539
539
540
540
Methods may call other methods by using method attributes of the ``self ``
@@ -581,7 +581,7 @@ this::
581
581
.
582
582
<statement-N>
583
583
584
- The name :class: `BaseClassName ` must be defined in a scope containing the
584
+ The name :class: `! BaseClassName ` must be defined in a scope containing the
585
585
derived class definition. In place of a base class name, other arbitrary
586
586
expressions are also allowed. This can be useful, for example, when the base
587
587
class is defined in another module::
@@ -644,9 +644,9 @@ multiple base classes looks like this::
644
644
For most purposes, in the simplest cases, you can think of the search for
645
645
attributes inherited from a parent class as depth-first, left-to-right, not
646
646
searching twice in the same class where there is an overlap in the hierarchy.
647
- Thus, if an attribute is not found in :class: `DerivedClassName `, it is searched
648
- for in :class: `Base1 `, then (recursively) in the base classes of :class: `Base1 `,
649
- and if it was not found there, it was searched for in :class: `Base2 `, and so on.
647
+ Thus, if an attribute is not found in :class: `! DerivedClassName `, it is searched
648
+ for in :class: `! Base1 `, then (recursively) in the base classes of :class: `! Base1 `,
649
+ and if it was not found there, it was searched for in :class: `! Base2 `, and so on.
650
650
651
651
In fact, it is slightly more complex than that; the method resolution order
652
652
changes dynamically to support cooperative calls to :func: `super `. This
@@ -759,7 +759,8 @@ is to use :mod:`dataclasses` for this purpose::
759
759
A piece of Python code that expects a particular abstract data type can often be
760
760
passed a class that emulates the methods of that data type instead. For
761
761
instance, if you have a function that formats some data from a file object, you
762
- can define a class with methods :meth: `read ` and :meth: `!readline ` that get the
762
+ can define a class with methods :meth: `~io.TextIOBase.read ` and
763
+ :meth: `~io.TextIOBase.readline ` that get the
763
764
data from a string buffer instead, and pass it as an argument.
764
765
765
766
.. (Unfortunately, this technique has its limitations: a class can't define
@@ -768,7 +769,7 @@ data from a string buffer instead, and pass it as an argument.
768
769
not cause the interpreter to read further input from it.)
769
770
770
771
Instance method objects have attributes, too: ``m.__self__ `` is the instance
771
- object with the method :meth: `m `, and ``m.__func__ `` is the function object
772
+ object with the method :meth: `! m `, and ``m.__func__ `` is the function object
772
773
corresponding to the method.
773
774
774
775
@@ -817,9 +818,9 @@ using the :func:`next` built-in function; this example shows how it all works::
817
818
StopIteration
818
819
819
820
Having seen the mechanics behind the iterator protocol, it is easy to add
820
- iterator behavior to your classes. Define an :meth: `__iter__ ` method which
821
+ iterator behavior to your classes. Define an :meth: `~container. __iter__ ` method which
821
822
returns an object with a :meth: `~iterator.__next__ ` method. If the class
822
- defines :meth: `__next__ `, then :meth: `__iter__ ` can just return ``self ``::
823
+ defines :meth: `! __next__ `, then :meth: `! __iter__ ` can just return ``self ``::
823
824
824
825
class Reverse:
825
826
"""Iterator for looping over a sequence backwards."""
@@ -878,7 +879,7 @@ easy to create::
878
879
879
880
Anything that can be done with generators can also be done with class-based
880
881
iterators as described in the previous section. What makes generators so
881
- compact is that the :meth: `__iter__ ` and :meth: `~generator.__next__ ` methods
882
+ compact is that the :meth: `~iterator. __iter__ ` and :meth: `~generator.__next__ ` methods
882
883
are created automatically.
883
884
884
885
Another key feature is that the local variables and execution state are
0 commit comments