@@ -1986,7 +1986,8 @@ Basic customization
1986
1986
"informal" string representation of instances of that class is required.
1987
1987
1988
1988
This is typically used for debugging, so it is important that the representation
1989
- is information-rich and unambiguous.
1989
+ is information-rich and unambiguous. A default implementation is provided by the
1990
+ :class: `object ` class itself.
1990
1991
1991
1992
.. index ::
1992
1993
single: string; __str__() (object method)
@@ -1996,10 +1997,10 @@ Basic customization
1996
1997
1997
1998
.. method :: object.__str__(self)
1998
1999
1999
- Called by :func: `str(object) <str> ` and the built-in functions
2000
- :func: ` format ` and :func: `print ` to compute the "informal" or nicely
2000
+ Called by :func: `str(object) <str> `, the default :meth: ` __format__ ` implementation,
2001
+ and the built-in function :func: `print `, to compute the "informal" or nicely
2001
2002
printable string representation of an object. The return value must be a
2002
- :ref: `string <textseq >` object.
2003
+ :ref: `str <textseq >` object.
2003
2004
2004
2005
This method differs from :meth: `object.__repr__ ` in that there is no
2005
2006
expectation that :meth: `__str__ ` return a valid Python expression: a more
@@ -2016,7 +2017,8 @@ Basic customization
2016
2017
.. index :: pair: built-in function; bytes
2017
2018
2018
2019
Called by :ref: `bytes <func-bytes >` to compute a byte-string representation
2019
- of an object. This should return a :class: `bytes ` object.
2020
+ of an object. This should return a :class: `bytes ` object. The :class: `object `
2021
+ class itself does not provide this method.
2020
2022
2021
2023
.. index ::
2022
2024
single: string; __format__() (object method)
@@ -2040,6 +2042,9 @@ Basic customization
2040
2042
2041
2043
The return value must be a string object.
2042
2044
2045
+ The default implementation by the :class: `object ` class should be given
2046
+ an empty *format_spec * string. It delegates to :meth: `__str__ `.
2047
+
2043
2048
.. versionchanged :: 3.4
2044
2049
The __format__ method of ``object `` itself raises a :exc: `TypeError `
2045
2050
if passed any non-empty string.
@@ -2082,6 +2087,12 @@ Basic customization
2082
2087
``(x<y or x==y) `` does not imply ``x<=y ``. To automatically generate ordering
2083
2088
operations from a single root operation, see :func: `functools.total_ordering `.
2084
2089
2090
+ By default, the :class: `object ` class provides implementations consistent
2091
+ with :ref: `expressions-value-comparisons `: equality compares according to
2092
+ object identity, and order comparisons raise :exc: `TypeError `. Each default
2093
+ method may generate these results directly, but may also return
2094
+ :data: `NotImplemented `.
2095
+
2085
2096
See the paragraph on :meth: `__hash__ ` for
2086
2097
some important notes on creating :term: `hashable ` objects which support
2087
2098
custom comparison operations and are usable as dictionary keys.
@@ -2137,9 +2148,9 @@ Basic customization
2137
2148
bucket).
2138
2149
2139
2150
User-defined classes have :meth: `__eq__ ` and :meth: `__hash__ ` methods
2140
- by default; with them, all objects compare unequal (except with themselves)
2141
- and ``x.__hash__() `` returns an appropriate value such that `` x == y ``
2142
- implies both that ``x is y `` and ``hash(x) == hash(y) ``.
2151
+ by default (inherited from the :class: ` object ` class) ; with them, all objects compare
2152
+ unequal (except with themselves) and ``x.__hash__() `` returns an appropriate
2153
+ value such that `` x == y `` implies both that ``x is y `` and ``hash(x) == hash(y) ``.
2143
2154
2144
2155
A class that overrides :meth: `__eq__ ` and does not define :meth: `__hash__ `
2145
2156
will have its :meth: `__hash__ ` implicitly set to ``None ``. When the
@@ -2189,8 +2200,8 @@ Basic customization
2189
2200
``bool() ``; should return ``False `` or ``True ``. When this method is not
2190
2201
defined, :meth: `~object.__len__ ` is called, if it is defined, and the object is
2191
2202
considered true if its result is nonzero. If a class defines neither
2192
- :meth: `!__len__ ` nor :meth: `!__bool__ `, all its instances are considered
2193
- true.
2203
+ :meth: `!__len__ ` nor :meth: `!__bool__ ` (which is true of the :class: ` object `
2204
+ class itself), all its instances are considered true.
2194
2205
2195
2206
2196
2207
.. _attribute-access :
@@ -2212,6 +2223,7 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
2212
2223
for ``self ``; or :meth: `__get__ ` of a *name * property raises
2213
2224
:exc: `AttributeError `). This method should either return the (computed)
2214
2225
attribute value or raise an :exc: `AttributeError ` exception.
2226
+ The :class: `object ` class itself does not provide this method.
2215
2227
2216
2228
Note that if the attribute is found through the normal mechanism,
2217
2229
:meth: `__getattr__ ` is not called. (This is an intentional asymmetry between
@@ -2350,8 +2362,8 @@ method (a so-called *descriptor* class) appears in an *owner* class (the
2350
2362
descriptor must be in either the owner's class dictionary or in the class
2351
2363
dictionary for one of its parents). In the examples below, "the attribute"
2352
2364
refers to the attribute whose name is the key of the property in the owner
2353
- class' :attr: `~object.__dict__ `.
2354
-
2365
+ class' :attr: `~object.__dict__ `. The :class: ` object ` class itself does not
2366
+ implement any of these protocols.
2355
2367
2356
2368
.. method :: object.__get__(self, instance, owner=None)
2357
2369
@@ -3043,17 +3055,19 @@ Emulating callable objects
3043
3055
3044
3056
Called when the instance is "called" as a function; if this method is defined,
3045
3057
``x(arg1, arg2, ...) `` roughly translates to ``type(x).__call__(x, arg1, ...) ``.
3058
+ The :class: `object ` class itself does not provide this method.
3046
3059
3047
3060
3048
3061
.. _sequence-types :
3049
3062
3050
3063
Emulating container types
3051
3064
-------------------------
3052
3065
3053
- The following methods can be defined to implement container objects. Containers
3054
- usually are :term: `sequences <sequence> ` (such as :class: `lists <list> ` or
3066
+ The following methods can be defined to implement container objects. None of them
3067
+ are provided by the :class: `object ` class itself. Containers usually are
3068
+ :term: `sequences <sequence> ` (such as :class: `lists <list> ` or
3055
3069
:class: `tuples <tuple> `) or :term: `mappings <mapping> ` (like
3056
- :class : `dictionaries <dict > `),
3070
+ :term : `dictionaries <dictionary > `),
3057
3071
but can represent other containers as well. The first set of methods is used
3058
3072
either to emulate a sequence or to emulate a mapping; the difference is that for
3059
3073
a sequence, the allowable keys should be the integers *k * for which ``0 <= k <
@@ -3416,6 +3430,7 @@ Typical uses of context managers include saving and restoring various kinds of
3416
3430
global state, locking and unlocking resources, closing opened files, etc.
3417
3431
3418
3432
For more information on context managers, see :ref: `typecontextmanager `.
3433
+ The :class: `object ` class itself does not provide the context manager methods.
3419
3434
3420
3435
3421
3436
.. method :: object.__enter__(self)
@@ -3620,6 +3635,8 @@ are awaitable.
3620
3635
Must return an :term: `iterator `. Should be used to implement
3621
3636
:term: `awaitable ` objects. For instance, :class: `asyncio.Future ` implements
3622
3637
this method to be compatible with the :keyword: `await ` expression.
3638
+ The :class: `object ` class itself is not awaitable and does not provide
3639
+ this method.
3623
3640
3624
3641
.. note ::
3625
3642
@@ -3705,6 +3722,9 @@ its ``__anext__`` method.
3705
3722
3706
3723
Asynchronous iterators can be used in an :keyword: `async for ` statement.
3707
3724
3725
+ The :class: `object ` class itself does not provide these methods.
3726
+
3727
+
3708
3728
.. method :: object.__aiter__(self)
3709
3729
3710
3730
Must return an *asynchronous iterator * object.
@@ -3751,6 +3771,8 @@ suspend execution in its ``__aenter__`` and ``__aexit__`` methods.
3751
3771
3752
3772
Asynchronous context managers can be used in an :keyword: `async with ` statement.
3753
3773
3774
+ The :class: `object ` class itself does not provide these methods.
3775
+
3754
3776
.. method :: object.__aenter__(self)
3755
3777
3756
3778
Semantically similar to :meth: `~object.__enter__ `, the only
0 commit comments