From 2c09057ce489c8c956adf1d2512ac13473d068cb Mon Sep 17 00:00:00 2001 From: furkanonder Date: Sat, 25 Mar 2023 23:53:45 +0300 Subject: [PATCH 01/24] add test for the predefined object's attributes --- Lib/test/test_class.py | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index 61df81b169775e..77a6047e1c85c1 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -503,6 +503,58 @@ def __eq__(self, other): return 1 self.assertRaises(TypeError, hash, C2()) + def testPredefinedAttrs(self): + o = object() + + class Custom: + pass + + c = Custom() + + methods = ( + "__new__", "__init__", + "__repr__", "__str__", "__format__", + "__eq__", "__ne__", "__lt__", "__le__", "__gt__", "__ge__", + "__hash__", + "__getattribute__", "__setattr__", "__delattr__", "__dir__", + ) + for name in methods: + with self.subTest(name): + self.assertTrue(callable(getattr(object, name))) + self.assertTrue(callable(getattr(o, name))) + self.assertTrue(callable(getattr(Custom, name))) + self.assertTrue(callable(getattr(c, name))) + + not_defined = [ + "__bool__", "__bytes__", "__del__", "__getattr__", "__len__", + "__get__", "__set__", "__delete__", "__objclass__", + "__len__", "__length_hint__", "__iter__", "__reversed__", + "__getitem__", "__setitem__", "__delitem__", + "__contains__", "__missing__", + "__divmod__", "__rdivmod__", + "__neg__", "__pos__", "__abs__", "__invert__", + "__complex__", "__int__", "__float__", "__round__", "__index__", + "__enter__", "__exit__", + "__await__", "__aiter__", "__anext__", "__aenter__", "__aexit__", + ] + augment = ( + "add", "sub", + "mul", "matmul", "truediv", "floordiv", "mod", "pow", + "lshift", "rshift", "and", "xor", "or", + ) + not_defined.extend(map("__{}__".format, augment)) + not_defined.extend(map("__r{}__".format, augment)) + not_defined.extend(map("__i{}__".format, augment)) + for name in not_defined: + with self.subTest(name): + self.assertFalse(hasattr(object, name)) + self.assertFalse(hasattr(o, name)) + self.assertFalse(hasattr(Custom, name)) + self.assertFalse(hasattr(c, name)) + + # __call__() is defined on the metaclass but not the class + self.assertFalse(hasattr(o, "__call__")) + self.assertFalse(hasattr(c, "__call__")) def testSFBug532646(self): # Test for SF bug 532646 From 9a71e3680f3f563bf6d11655a62aed38e18a2543 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Sat, 25 Mar 2023 23:54:04 +0300 Subject: [PATCH 02/24] Include the "object" type in the lists of documented types --- Doc/library/functions.rst | 7 ++-- Doc/library/stdtypes.rst | 3 +- Doc/reference/datamodel.rst | 84 +++++++++++++++++++++++++------------ 3 files changed, 64 insertions(+), 30 deletions(-) diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index f0f374771b0cf1..bea29c441b1ca3 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -1117,9 +1117,10 @@ are always available. They are listed here in alphabetical order. .. class:: object() - Return a new featureless object. :class:`object` is a base for all classes. - It has methods that are common to all instances of Python classes. This - function does not accept any arguments. + This is the ultimate base class of all other classes. It has methods + that are common to all instances of Python classes. When the constructor + is called, it returns a new featureless object. The constructor does not + accept any arguments. .. note:: diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index bcfc6e5cfce611..e9972a3435719c 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -5207,7 +5207,8 @@ instantiated from the type:: Other Built-in Types ==================== -The interpreter supports several other kinds of objects. Most of these support +The interpreter supports several other kinds of objects, as well as direct +instances of :class:`object`, the universal type. Most of these support only one or two operations. diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 1865d09fcaa127..bcb08cf902a8b1 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -126,11 +126,13 @@ The standard type hierarchy pair: extension; module pair: C; language -Below is a list of the types that are built into Python. Extension modules -(written in C, Java, or other languages, depending on the implementation) can -define additional types. Future versions of Python may add types to the type -hierarchy (e.g., rational numbers, efficiently stored arrays of integers, etc.), -although such additions will often be provided via the standard library instead. +Below is a list of the types that are built into Python. The fundamental type +is :class:`object`; all other types, whether built-in or not, are derived +from it. Extension modules (written in C, Java, or other languages, +depending on the implementation) can define additional types. Future versions +of Python may add types to the type hierarchy (e.g., rational numbers, +efficiently stored arrays of integers, etc.), although such additions will +often be provided via the standard library instead. .. index:: single: attribute @@ -1269,10 +1271,11 @@ Basic customization class). The return value of :meth:`__new__` should be the new object instance (usually an instance of *cls*). - Typical implementations create a new instance of the class by invoking the + Typical custom implementations create a new instance of the class by invoking the superclass's :meth:`__new__` method using ``super().__new__(cls[, ...])`` - with appropriate arguments and then modifying the newly created instance - as necessary before returning it. + with appropriate arguments and then modify the newly-created instance as necessary + before returning it. The base implementation in :class:`object` should only be + passed the *cls* argument, and returns an uninitialized instance of that class. If :meth:`__new__` is invoked during object construction and it returns an instance of *cls*, then the new instance’s :meth:`__init__` method @@ -1294,9 +1297,14 @@ Basic customization Called after the instance has been created (by :meth:`__new__`), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an :meth:`__init__` - method, the derived class's :meth:`__init__` method, if any, must explicitly + method, the derived class's :meth:`__init__` method, if any, may need to call it to ensure proper initialization of the base class part of the instance; for example: ``super().__init__([args...])``. + Any :meth:`__init__` method that is overridden is not implicitly called. + + A base implementation in :class:`object` is provided, but it does not + need to be called. If it is only passed the *self* argument, it has + no effect. Because :meth:`__new__` and :meth:`__init__` work together in constructing objects (:meth:`__new__` to create it, and :meth:`__init__` to customize it), @@ -1315,7 +1323,8 @@ Basic customization finalizer or (improperly) a destructor. If a base class has a :meth:`__del__` method, the derived class's :meth:`__del__` method, if any, must explicitly call it to ensure proper deletion of the base - class part of the instance. + class part of the instance.The :class:`object` class itself does not have a + :meth:`__del__` method. It is possible (though not recommended!) for the :meth:`__del__` method to postpone destruction of the instance by creating a new reference to @@ -1383,7 +1392,8 @@ Basic customization "informal" string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation - is information-rich and unambiguous. + is information-rich and unambiguous. A default implementation is provided by the + :class:`object` class itself. .. index:: single: string; __str__() (object method) @@ -1393,8 +1403,8 @@ Basic customization .. method:: object.__str__(self) - Called by :func:`str(object) ` and the built-in functions - :func:`format` and :func:`print` to compute the "informal" or nicely + Called by :func:`str(object) `, the default :meth:`__format__` implementation, + and the built-in function :func:`print`, to compute the "informal" or nicely printable string representation of an object. The return value must be a :ref:`string ` object. @@ -1413,7 +1423,8 @@ Basic customization .. index:: builtin: bytes Called by :ref:`bytes ` to compute a byte-string representation - of an object. This should return a :class:`bytes` object. + of an object. This should return a :class:`bytes` object. The :class:`object` + class itself does not provide this method. .. index:: single: string; __format__() (object method) @@ -1437,6 +1448,9 @@ Basic customization The return value must be a string object. + The default implementation by the ``object`` class should be given + an empty ``format_spec`` string. It delegates to :meth:`__str__` + .. versionchanged:: 3.4 The __format__ method of ``object`` itself raises a :exc:`TypeError` if passed any non-empty string. @@ -1479,6 +1493,11 @@ Basic customization ``(x` (such as :class:`lists ` or +The following methods can be defined to implement container objects. None of them +are provided by the :class:`object` class itself. Containers usually are +:term:`sequences ` (such as :class:`lists ` or :class:`tuples `) or :term:`mappings ` (like :class:`dictionaries `), but can represent other containers as well. The first set of methods is used @@ -2789,6 +2813,7 @@ Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see :ref:`typecontextmanager`. +The :class:`object` class itself does not provide the context manager methods. .. method:: object.__enter__(self) @@ -2952,6 +2977,8 @@ are awaitable. Must return an :term:`iterator`. Should be used to implement :term:`awaitable` objects. For instance, :class:`asyncio.Future` implements this method to be compatible with the :keyword:`await` expression. + The :class:`object` class itself is not awaitable and does not provide + this method. .. note:: @@ -3037,6 +3064,9 @@ its ``__anext__`` method. Asynchronous iterators can be used in an :keyword:`async for` statement. +The :class:`object` class itself does not provide these methods. + + .. method:: object.__aiter__(self) Must return an *asynchronous iterator* object. @@ -3083,6 +3113,8 @@ suspend execution in its ``__aenter__`` and ``__aexit__`` methods. Asynchronous context managers can be used in an :keyword:`async with` statement. +The :class:`object` class itself does not provide these methods. + .. method:: object.__aenter__(self) Semantically similar to :meth:`__enter__`, the only From d327409bed3e6592306bdea7241e33cdfafff199 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Wed, 29 Mar 2023 00:52:30 +0300 Subject: [PATCH 03/24] remove 'or' from augment tuple --- Lib/test/test_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index 77a6047e1c85c1..909652689910af 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -540,7 +540,7 @@ class Custom: augment = ( "add", "sub", "mul", "matmul", "truediv", "floordiv", "mod", "pow", - "lshift", "rshift", "and", "xor", "or", + "lshift", "rshift", "and", "xor", ) not_defined.extend(map("__{}__".format, augment)) not_defined.extend(map("__r{}__".format, augment)) From 73698a507af4f9ebc737f566f8a47072c427eb1b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 28 Mar 2023 22:24:48 +0000 Subject: [PATCH 04/24] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Documentation/2023-03-28-22-24-45.gh-issue-60712.So5uad.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2023-03-28-22-24-45.gh-issue-60712.So5uad.rst diff --git a/Misc/NEWS.d/next/Documentation/2023-03-28-22-24-45.gh-issue-60712.So5uad.rst b/Misc/NEWS.d/next/Documentation/2023-03-28-22-24-45.gh-issue-60712.So5uad.rst new file mode 100644 index 00000000000000..e6b452cd5090cc --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2023-03-28-22-24-45.gh-issue-60712.So5uad.rst @@ -0,0 +1 @@ +Include the "object" type in the lists of documented types. Co-Authored-By: @cjerdonek From 106406564d9cd2f4b5757ec7c52dd518ee270b53 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Thu, 30 Mar 2023 12:48:37 +0300 Subject: [PATCH 05/24] Add cross-reference to news Co-authored-by: C.A.M. Gerlach --- .../2023-03-28-22-24-45.gh-issue-60712.So5uad.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Documentation/2023-03-28-22-24-45.gh-issue-60712.So5uad.rst b/Misc/NEWS.d/next/Documentation/2023-03-28-22-24-45.gh-issue-60712.So5uad.rst index e6b452cd5090cc..e401cc2535e389 100644 --- a/Misc/NEWS.d/next/Documentation/2023-03-28-22-24-45.gh-issue-60712.So5uad.rst +++ b/Misc/NEWS.d/next/Documentation/2023-03-28-22-24-45.gh-issue-60712.So5uad.rst @@ -1 +1,2 @@ -Include the "object" type in the lists of documented types. Co-Authored-By: @cjerdonek +Include the :class:`object` type in the lists of documented types. +Change by Furkan Onder and Martin Panter. From ce4d192c403e05a2e989d5e08eab78dc0e5fba21 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Thu, 30 Mar 2023 12:54:57 +0300 Subject: [PATCH 06/24] Fix format for the function parameter Co-authored-by: C.A.M. Gerlach --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index bcb08cf902a8b1..2c6629ebad1910 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1449,7 +1449,7 @@ Basic customization The return value must be a string object. The default implementation by the ``object`` class should be given - an empty ``format_spec`` string. It delegates to :meth:`__str__` + an empty *format_spec* string. It delegates to :meth:`__str__` .. versionchanged:: 3.4 The __format__ method of ``object`` itself raises a :exc:`TypeError` From aca2c042ac42e475d37ed3ad56705fb757bc0096 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Thu, 30 Mar 2023 12:55:53 +0300 Subject: [PATCH 07/24] Add space Co-authored-by: C.A.M. Gerlach --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 2c6629ebad1910..0aa63facdbe736 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1323,7 +1323,7 @@ Basic customization finalizer or (improperly) a destructor. If a base class has a :meth:`__del__` method, the derived class's :meth:`__del__` method, if any, must explicitly call it to ensure proper deletion of the base - class part of the instance.The :class:`object` class itself does not have a + class part of the instance. The :class:`object` class itself does not have a :meth:`__del__` method. It is possible (though not recommended!) for the :meth:`__del__` method From f6c12d80c55d670b55102322554e917348d0ce01 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Thu, 30 Mar 2023 13:04:45 +0300 Subject: [PATCH 08/24] add reference for the 'object' Co-authored-by: C.A.M. Gerlach --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 0aa63facdbe736..538a8b1032f1b8 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1448,7 +1448,7 @@ Basic customization The return value must be a string object. - The default implementation by the ``object`` class should be given + The default implementation by the :class:`object` class should be given an empty *format_spec* string. It delegates to :meth:`__str__` .. versionchanged:: 3.4 From b1ab4a22aba75946b8a8c1825b6c2a806ce0fd4e Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Thu, 30 Mar 2023 13:15:05 +0300 Subject: [PATCH 09/24] add reference for NotImplemented Co-authored-by: C.A.M. Gerlach --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 538a8b1032f1b8..a382a1928e89ad 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1496,7 +1496,7 @@ Basic customization By default, the :class:`object` class provides implementations consistent with :ref:`comparisons`: equality compares according to object identity, and order comparisons raise :exc:`TypeError`. Each default method may generate - these results directly, but may also return ``NotImplemented``. + these results directly, but may also return :data:`NotImplemented`. See the paragraph on :meth:`__hash__` for some important notes on creating :term:`hashable` objects which support From d70994650c7cd11ff503a2711186ba7923e25bbc Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Sat, 1 Apr 2023 01:12:36 +0300 Subject: [PATCH 10/24] Change ref:`string ` as class:`str` Co-authored-by: C.A.M. Gerlach --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index a382a1928e89ad..37cdca9cc53525 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1406,7 +1406,7 @@ Basic customization Called by :func:`str(object) `, the default :meth:`__format__` implementation, and the built-in function :func:`print`, to compute the "informal" or nicely printable string representation of an object. The return value must be a - :ref:`string ` object. + :class:`str` object. This method differs from :meth:`object.__repr__` in that there is no expectation that :meth:`__str__` return a valid Python expression: a more From 105f33137069ebc68bf9c38e5bc970ae954c195a Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Sat, 1 Apr 2023 01:14:08 +0300 Subject: [PATCH 11/24] remove hyphen from `newly-created` --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 37cdca9cc53525..7b56548b3d5fd8 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1273,7 +1273,7 @@ Basic customization Typical custom implementations create a new instance of the class by invoking the superclass's :meth:`__new__` method using ``super().__new__(cls[, ...])`` - with appropriate arguments and then modify the newly-created instance as necessary + with appropriate arguments and then modify the newly created instance as necessary before returning it. The base implementation in :class:`object` should only be passed the *cls* argument, and returns an uninitialized instance of that class. From 119e29b226eb68e7e1c9e1dbfcf8e88bf9182241 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sun, 18 Jun 2023 17:42:07 -0400 Subject: [PATCH 12/24] Update Doc/reference/datamodel.rst 'dictionaries' to 'dict' Co-authored-by: C.A.M. Gerlach --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 7b56548b3d5fd8..53ace64e3e6c83 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -2453,7 +2453,7 @@ The following methods can be defined to implement container objects. None of the are provided by the :class:`object` class itself. Containers usually are :term:`sequences ` (such as :class:`lists ` or :class:`tuples `) or :term:`mappings ` (like -:class:`dictionaries `), +:term:`dictionaries `), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which ``0 <= k < From 7762a08cc48586aaf5e54da9920ea0faa252e2a9 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Thu, 18 Jul 2024 01:51:54 +0300 Subject: [PATCH 13/24] Update predefined attribute types in testPredefinedAttrs --- Lib/test/test_class.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index 4d914b72652436..6702e0ff7bbddb 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -511,35 +511,33 @@ class Custom: c = Custom() methods = ( - "__new__", "__init__", - "__repr__", "__str__", "__format__", - "__eq__", "__ne__", "__lt__", "__le__", "__gt__", "__ge__", - "__hash__", - "__getattribute__", "__setattr__", "__delattr__", "__dir__", + '__class__', '__delattr__', '__dir__', '__eq__', '__format__', + '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', + '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', + '__new__', '__reduce__', '__reduce_ex__', '__repr__', + '__setattr__', '__sizeof__', '__str__', '__subclasshook__' ) for name in methods: with self.subTest(name): - self.assertTrue(callable(getattr(object, name))) - self.assertTrue(callable(getattr(o, name))) - self.assertTrue(callable(getattr(Custom, name))) - self.assertTrue(callable(getattr(c, name))) + self.assertTrue(callable(getattr(object, name, None))) + self.assertTrue(callable(getattr(o, name, None))) + self.assertTrue(callable(getattr(Custom, name, None))) + self.assertTrue(callable(getattr(c, name, None))) not_defined = [ - "__bool__", "__bytes__", "__del__", "__getattr__", "__len__", - "__get__", "__set__", "__delete__", "__objclass__", - "__len__", "__length_hint__", "__iter__", "__reversed__", - "__getitem__", "__setitem__", "__delitem__", - "__contains__", "__missing__", - "__divmod__", "__rdivmod__", - "__neg__", "__pos__", "__abs__", "__invert__", - "__complex__", "__int__", "__float__", "__round__", "__index__", - "__enter__", "__exit__", - "__await__", "__aiter__", "__anext__", "__aenter__", "__aexit__", + '__abs__', '__aenter__', '__aexit__', '__aiter__', '__anext__', + '__await__', '__bool__', '__bytes__', '__ceil__', + '__complex__', '__contains__', '__del__', '__delete__', + '__delitem__', '__divmod__', '__enter__', '__exit__', + '__float__', '__floor__', '__get__', '__getattr__', '__getitem__', + '__index__', '__int__', '__invert__', '__iter__', '__len__', + '__length_hint__', '__missing__', '__neg__', '__next__', + '__objclass__', '__pos__', '__rdivmod__', '__reversed__', + '__round__', '__set__', '__setitem__', '__trunc__' ] augment = ( - "add", "sub", - "mul", "matmul", "truediv", "floordiv", "mod", "pow", - "lshift", "rshift", "and", "xor", + 'add', 'and', 'floordiv', 'lshift', 'matmul', 'mod', 'mul', 'pow', + 'rshift', 'sub', 'truediv', 'xor' ) not_defined.extend(map("__{}__".format, augment)) not_defined.extend(map("__r{}__".format, augment)) From 7ffed7c6315fe86571c57e98d34a4a9df3d957ea Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Thu, 18 Jul 2024 02:08:37 +0300 Subject: [PATCH 14/24] Change `universal type` as `top type` --- Doc/library/stdtypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index c39ed3f324001d..4046a797c293a4 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -5273,7 +5273,7 @@ Other Built-in Types ==================== The interpreter supports several other kinds of objects, as well as direct -instances of :class:`object`, the universal type. Most of these support +instances of :class:`object`, the top type. Most of these support only one or two operations. From 71a2d433deb86504e2f65de3a0882dc5cfbfbd38 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Sun, 11 Aug 2024 22:44:52 +0300 Subject: [PATCH 15/24] Don't mention about the top type --- Doc/library/stdtypes.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 280aa4f622105d..34e5d615dd92b8 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -5312,9 +5312,9 @@ instantiated from the type:: Other Built-in Types ==================== -The interpreter supports several other kinds of objects, as well as direct -instances of :class:`object`, the top type. Most of these support -only one or two operations. +The interpreter supports several other kinds of objects, including instances +of the :class:`object` itself. Most of these support only one or two +operations. .. _typesmodules: From 2d9f0a0af36c497fb1e8c744b50e4a7c6c23cbd6 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Sun, 11 Aug 2024 22:49:15 +0300 Subject: [PATCH 16/24] Update the description of richcmpfuncs --- Doc/reference/datamodel.rst | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index dfb38a59c90adc..60ed1c39573f9a 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1792,7 +1792,7 @@ Basic customization The return value must be a string object. The default implementation by the :class:`object` class should be given - an empty *format_spec* string. It delegates to :meth:`__str__` + an empty *format_spec* string. It delegates to :meth:`__str__`. .. versionchanged:: 3.4 The __format__ method of ``object`` itself raises a :exc:`TypeError` @@ -1837,9 +1837,10 @@ Basic customization operations from a single root operation, see :func:`functools.total_ordering`. By default, the :class:`object` class provides implementations consistent - with :ref:`comparisons`: equality compares according to object identity, - and order comparisons raise :exc:`TypeError`. Each default method may generate - these results directly, but may also return :data:`NotImplemented`. + with :ref:`expressions-value-comparisons`: equality compares according to + object identity, and order comparisons raise :exc:`TypeError`. Each default + method may generate these results directly, but may also return + :data:`NotImplemented`. See the paragraph on :meth:`__hash__` for some important notes on creating :term:`hashable` objects which support @@ -2041,8 +2042,8 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances. .. method:: object.__dir__(self) Called when :func:`dir` is called on the object. An iterable must be - returned. The :func:`dir` converts the returned iterable to a list and - sorts it. The :class:`object` class itself provides an implementation + returned. The :func:`dir` function converts the returned iterable to a list + and sorts it. The :class:`object` class itself provides an implementation consistent with the :func:`dir` behaviour for arbitrary objects. @@ -2112,7 +2113,7 @@ method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner's class dictionary or in the class dictionary for one of its parents). In the examples below, "the attribute" refers to the attribute whose name is the key of the property in the owner -class' :attr:`__dict__`. The :class:`object` class itself does not +class' :attr:`~object.__dict__`. The :class:`object` class itself does not implement any of these protocols. .. method:: object.__get__(self, instance, owner=None) From 10adcee244078721be5770869e30f4f9c4be360a Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Mon, 12 Aug 2024 18:27:02 +0300 Subject: [PATCH 17/24] Update Doc/library/stdtypes.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric --- Doc/library/stdtypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 34e5d615dd92b8..eac2d3a3e874e8 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -5313,7 +5313,7 @@ Other Built-in Types ==================== The interpreter supports several other kinds of objects, including instances -of the :class:`object` itself. Most of these support only one or two +of the :class:`object` class itself. Most of these support only one or two operations. From ab5ef405501adfedcef7e3f50f30d224b60c82ba Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Fri, 18 Oct 2024 02:09:04 +0300 Subject: [PATCH 18/24] Revert: Hierarchy Section in Data Model Documentation --- Doc/reference/datamodel.rst | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index a87aefa3bc3afb..be8aa460c76b95 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -130,13 +130,11 @@ The standard type hierarchy pair: extension; module pair: C; language -Below is a list of the types that are built into Python. The fundamental type -is :class:`object`; all other types, whether built-in or not, are derived -from it. Extension modules (written in C, Java, or other languages, -depending on the implementation) can define additional types. Future versions -of Python may add types to the type hierarchy (e.g., rational numbers, -efficiently stored arrays of integers, etc.), although such additions will -often be provided via the standard library instead. +Below is a list of the types that are built into Python. Extension modules +(written in C, Java, or other languages, depending on the implementation) can +define additional types. Future versions of Python may add types to the type +hierarchy (e.g., rational numbers, efficiently stored arrays of integers, etc.), +although such additions will often be provided via the standard library instead. .. index:: single: attribute From 6681f671247c02916fb3da8a5a0f79a3ba374ac5 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Mon, 21 Oct 2024 01:38:51 +0300 Subject: [PATCH 19/24] Revert to original explanations of __new__ and __init__ methods in datamodel.rst for improved clarity. --- Doc/reference/datamodel.rst | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index be8aa460c76b95..eec1a3559c6705 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1609,9 +1609,8 @@ Basic customization Typical custom implementations create a new instance of the class by invoking the superclass's :meth:`__new__` method using ``super().__new__(cls[, ...])`` - with appropriate arguments and then modify the newly created instance as necessary - before returning it. The base implementation in :class:`object` should only be - passed the *cls* argument, and returns an uninitialized instance of that class. + with appropriate arguments and then modifying the newly created instance + as necessary before returning it. If :meth:`__new__` is invoked during object construction and it returns an instance of *cls*, then the new instance’s :meth:`__init__` method @@ -1633,14 +1632,9 @@ Basic customization Called after the instance has been created (by :meth:`__new__`), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an :meth:`__init__` - method, the derived class's :meth:`__init__` method, if any, may need to + method, the derived class's :meth:`__init__` method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: ``super().__init__([args...])``. - Any :meth:`__init__` method that is overridden is not implicitly called. - - A base implementation in :class:`object` is provided, but it does not - need to be called. If it is only passed the *self* argument, it has - no effect. Because :meth:`__new__` and :meth:`__init__` work together in constructing objects (:meth:`__new__` to create it, and :meth:`__init__` to customize it), @@ -1659,8 +1653,7 @@ Basic customization finalizer or (improperly) a destructor. If a base class has a :meth:`__del__` method, the derived class's :meth:`__del__` method, if any, must explicitly call it to ensure proper deletion of the base - class part of the instance. The :class:`object` class itself does not have a - :meth:`__del__` method. + class part of the instance. It is possible (though not recommended!) for the :meth:`__del__` method to postpone destruction of the instance by creating a new reference to @@ -2037,9 +2030,7 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances. .. method:: object.__dir__(self) Called when :func:`dir` is called on the object. An iterable must be - returned. The :func:`dir` function converts the returned iterable to a list - and sorts it. The :class:`object` class itself provides an implementation - consistent with the :func:`dir` behaviour for arbitrary objects. + returned. :func:`dir` converts the returned iterable to a list and sorts it. Customizing module attribute access From 3f41130359c2ed45d96b779ca790dd85dce5db85 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Mon, 21 Oct 2024 15:54:28 +0300 Subject: [PATCH 20/24] Update Doc/reference/datamodel.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 313b66eb3b6dda..bf5e19c20bba09 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1917,7 +1917,7 @@ Basic customization class). The return value of :meth:`__new__` should be the new object instance (usually an instance of *cls*). - Typical custom implementations create a new instance of the class by invoking the + Typical implementations create a new instance of the class by invoking the superclass's :meth:`__new__` method using ``super().__new__(cls[, ...])`` with appropriate arguments and then modifying the newly created instance as necessary before returning it. From 026e514db15e0ea4a1fb0f7d0e9173514bc15bed Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Tue, 22 Oct 2024 01:39:21 +0300 Subject: [PATCH 21/24] Remove blank line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric --- Doc/reference/datamodel.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index bf5e19c20bba09..6c18b8fd0f49b4 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -3104,6 +3104,7 @@ Emulating callable objects ``x(arg1, arg2, ...)`` roughly translates to ``type(x).__call__(x, arg1, ...)``. The :class:`object` class itself does not provide this method. + .. _sequence-types: Emulating container types From 71810461bfc62c83a12275b8d8f218ecd32241c6 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Tue, 22 Oct 2024 01:42:52 +0300 Subject: [PATCH 22/24] Use ref:`str ` instead of :class:`str MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 6c18b8fd0f49b4..3136f07c942359 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -2047,7 +2047,7 @@ Basic customization Called by :func:`str(object) `, the default :meth:`__format__` implementation, and the built-in function :func:`print`, to compute the "informal" or nicely printable string representation of an object. The return value must be a - :class:`str` object. + :ref:`str ` object. This method differs from :meth:`object.__repr__` in that there is no expectation that :meth:`__str__` return a valid Python expression: a more From 55f8c6ae95b4de3d40f24d9eca586d44f1052e87 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Tue, 22 Oct 2024 02:14:32 +0300 Subject: [PATCH 23/24] Revert changes the description of Other Built-in Types in stdtypes.rst --- Doc/library/stdtypes.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index a9c5cff25e6ee4..a6e2e3b8928ebe 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -5314,9 +5314,8 @@ instantiated from the type:: Other Built-in Types ==================== -The interpreter supports several other kinds of objects, including instances -of the :class:`object` class itself. Most of these support only one or two -operations. +The interpreter supports several other kinds of objects. Most of these support +only one or two operations. .. _typesmodules: From b19974615cca60d3a316614f8ffa222cbf6fb1f5 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Tue, 22 Oct 2024 23:07:16 +0300 Subject: [PATCH 24/24] Update Doc/reference/datamodel.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric --- Doc/reference/datamodel.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 3136f07c942359..3688e308d1d8fb 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -2194,8 +2194,8 @@ Basic customization immutable (if the object's hash value changes, it will be in the wrong hash bucket). - User-defined classes (and the :class:`object` class itself) have :meth:`__eq__` - and :meth:`__hash__` methods by default; with them, all objects compare + User-defined classes have :meth:`__eq__` and :meth:`__hash__` methods + by default (inherited from the :class:`object` class); with them, all objects compare unequal (except with themselves) and ``x.__hash__()`` returns an appropriate value such that ``x == y`` implies both that ``x is y`` and ``hash(x) == hash(y)``.