@@ -1942,7 +1942,8 @@ Basic customization
1942
1942
"informal" string representation of instances of that class is required.
1943
1943
1944
1944
This is typically used for debugging, so it is important that the representation
1945
- is information-rich and unambiguous.
1945
+ is information-rich and unambiguous. A default implementation is provided by the
1946
+ :class: `object ` class itself.
1946
1947
1947
1948
.. index ::
1948
1949
single: string; __str__() (object method)
@@ -1952,10 +1953,10 @@ Basic customization
1952
1953
1953
1954
.. method :: object.__str__(self)
1954
1955
1955
- Called by :func: `str(object) <str> ` and the built-in functions
1956
- :func: ` format ` and :func: `print ` to compute the "informal" or nicely
1956
+ Called by :func: `str(object) <str> `, the default :meth: ` __format__ ` implementation,
1957
+ and the built-in function :func: `print `, to compute the "informal" or nicely
1957
1958
printable string representation of an object. The return value must be a
1958
- :ref: `string <textseq >` object.
1959
+ :ref: `str <textseq >` object.
1959
1960
1960
1961
This method differs from :meth: `object.__repr__ ` in that there is no
1961
1962
expectation that :meth: `__str__ ` return a valid Python expression: a more
@@ -1972,7 +1973,8 @@ Basic customization
1972
1973
.. index :: pair: built-in function; bytes
1973
1974
1974
1975
Called by :ref: `bytes <func-bytes >` to compute a byte-string representation
1975
- of an object. This should return a :class: `bytes ` object.
1976
+ of an object. This should return a :class: `bytes ` object. The :class: `object `
1977
+ class itself does not provide this method.
1976
1978
1977
1979
.. index ::
1978
1980
single: string; __format__() (object method)
@@ -1996,6 +1998,9 @@ Basic customization
1996
1998
1997
1999
The return value must be a string object.
1998
2000
2001
+ The default implementation by the :class: `object ` class should be given
2002
+ an empty *format_spec * string. It delegates to :meth: `__str__ `.
2003
+
1999
2004
.. versionchanged :: 3.4
2000
2005
The __format__ method of ``object `` itself raises a :exc: `TypeError `
2001
2006
if passed any non-empty string.
@@ -2038,6 +2043,12 @@ Basic customization
2038
2043
``(x<y or x==y) `` does not imply ``x<=y ``. To automatically generate ordering
2039
2044
operations from a single root operation, see :func: `functools.total_ordering `.
2040
2045
2046
+ By default, the :class: `object ` class provides implementations consistent
2047
+ with :ref: `expressions-value-comparisons `: equality compares according to
2048
+ object identity, and order comparisons raise :exc: `TypeError `. Each default
2049
+ method may generate these results directly, but may also return
2050
+ :data: `NotImplemented `.
2051
+
2041
2052
See the paragraph on :meth: `__hash__ ` for
2042
2053
some important notes on creating :term: `hashable ` objects which support
2043
2054
custom comparison operations and are usable as dictionary keys.
@@ -2093,9 +2104,9 @@ Basic customization
2093
2104
bucket).
2094
2105
2095
2106
User-defined classes have :meth: `__eq__ ` and :meth: `__hash__ ` methods
2096
- by default; with them, all objects compare unequal (except with themselves)
2097
- and ``x.__hash__() `` returns an appropriate value such that `` x == y ``
2098
- implies both that ``x is y `` and ``hash(x) == hash(y) ``.
2107
+ by default (inherited from the :class: ` object ` class) ; with them, all objects compare
2108
+ unequal (except with themselves) and ``x.__hash__() `` returns an appropriate
2109
+ value such that `` x == y `` implies both that ``x is y `` and ``hash(x) == hash(y) ``.
2099
2110
2100
2111
A class that overrides :meth: `__eq__ ` and does not define :meth: `__hash__ `
2101
2112
will have its :meth: `__hash__ ` implicitly set to ``None ``. When the
@@ -2145,8 +2156,8 @@ Basic customization
2145
2156
``bool() ``; should return ``False `` or ``True ``. When this method is not
2146
2157
defined, :meth: `~object.__len__ ` is called, if it is defined, and the object is
2147
2158
considered true if its result is nonzero. If a class defines neither
2148
- :meth: `!__len__ ` nor :meth: `!__bool__ `, all its instances are considered
2149
- true.
2159
+ :meth: `!__len__ ` nor :meth: `!__bool__ ` (which is true of the :class: ` object `
2160
+ class itself), all its instances are considered true.
2150
2161
2151
2162
2152
2163
.. _attribute-access :
@@ -2168,6 +2179,7 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
2168
2179
for ``self ``; or :meth: `__get__ ` of a *name * property raises
2169
2180
:exc: `AttributeError `). This method should either return the (computed)
2170
2181
attribute value or raise an :exc: `AttributeError ` exception.
2182
+ The :class: `object ` class itself does not provide this method.
2171
2183
2172
2184
Note that if the attribute is found through the normal mechanism,
2173
2185
:meth: `__getattr__ ` is not called. (This is an intentional asymmetry between
@@ -2306,8 +2318,8 @@ method (a so-called *descriptor* class) appears in an *owner* class (the
2306
2318
descriptor must be in either the owner's class dictionary or in the class
2307
2319
dictionary for one of its parents). In the examples below, "the attribute"
2308
2320
refers to the attribute whose name is the key of the property in the owner
2309
- class' :attr: `~object.__dict__ `.
2310
-
2321
+ class' :attr: `~object.__dict__ `. The :class: ` object ` class itself does not
2322
+ implement any of these protocols.
2311
2323
2312
2324
.. method :: object.__get__(self, instance, owner=None)
2313
2325
@@ -2999,17 +3011,19 @@ Emulating callable objects
2999
3011
3000
3012
Called when the instance is "called" as a function; if this method is defined,
3001
3013
``x(arg1, arg2, ...) `` roughly translates to ``type(x).__call__(x, arg1, ...) ``.
3014
+ The :class: `object ` class itself does not provide this method.
3002
3015
3003
3016
3004
3017
.. _sequence-types :
3005
3018
3006
3019
Emulating container types
3007
3020
-------------------------
3008
3021
3009
- The following methods can be defined to implement container objects. Containers
3010
- usually are :term: `sequences <sequence> ` (such as :class: `lists <list> ` or
3022
+ The following methods can be defined to implement container objects. None of them
3023
+ are provided by the :class: `object ` class itself. Containers usually are
3024
+ :term: `sequences <sequence> ` (such as :class: `lists <list> ` or
3011
3025
:class: `tuples <tuple> `) or :term: `mappings <mapping> ` (like
3012
- :class : `dictionaries <dict > `),
3026
+ :term : `dictionaries <dictionary > `),
3013
3027
but can represent other containers as well. The first set of methods is used
3014
3028
either to emulate a sequence or to emulate a mapping; the difference is that for
3015
3029
a sequence, the allowable keys should be the integers *k * for which ``0 <= k <
@@ -3372,6 +3386,7 @@ Typical uses of context managers include saving and restoring various kinds of
3372
3386
global state, locking and unlocking resources, closing opened files, etc.
3373
3387
3374
3388
For more information on context managers, see :ref: `typecontextmanager `.
3389
+ The :class: `object ` class itself does not provide the context manager methods.
3375
3390
3376
3391
3377
3392
.. method :: object.__enter__(self)
@@ -3576,6 +3591,8 @@ are awaitable.
3576
3591
Must return an :term: `iterator `. Should be used to implement
3577
3592
:term: `awaitable ` objects. For instance, :class: `asyncio.Future ` implements
3578
3593
this method to be compatible with the :keyword: `await ` expression.
3594
+ The :class: `object ` class itself is not awaitable and does not provide
3595
+ this method.
3579
3596
3580
3597
.. note ::
3581
3598
@@ -3661,6 +3678,9 @@ its ``__anext__`` method.
3661
3678
3662
3679
Asynchronous iterators can be used in an :keyword: `async for ` statement.
3663
3680
3681
+ The :class: `object ` class itself does not provide these methods.
3682
+
3683
+
3664
3684
.. method :: object.__aiter__(self)
3665
3685
3666
3686
Must return an *asynchronous iterator * object.
@@ -3707,6 +3727,8 @@ suspend execution in its ``__aenter__`` and ``__aexit__`` methods.
3707
3727
3708
3728
Asynchronous context managers can be used in an :keyword: `async with ` statement.
3709
3729
3730
+ The :class: `object ` class itself does not provide these methods.
3731
+
3710
3732
.. method :: object.__aenter__(self)
3711
3733
3712
3734
Semantically similar to :meth: `~object.__enter__ `, the only
0 commit comments