From dc07e9cd02da5e5bbf4c583a6e67affc6b0bb698 Mon Sep 17 00:00:00 2001 From: Ievgen Kapinos Date: Sun, 12 Oct 2025 15:25:47 +0200 Subject: [PATCH 1/5] Added CatDog example --- Doc/tutorial/classes.rst | 62 +++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 9ab003d5cd3dd5..29149d5af856f7 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -638,33 +638,43 @@ multiple base classes looks like this:: For most purposes, in the simplest cases, you can think of the search for -attributes inherited from a parent class as depth-first, left-to-right, not +attributes inherited from a parent class as breadth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy. -Thus, if an attribute is not found in :class:`!DerivedClassName`, it is searched -for in :class:`!Base1`, then (recursively) in the base classes of :class:`!Base1`, -and if it was not found there, it was searched for in :class:`!Base2`, and so on. - -In fact, it is slightly more complex than that; the method resolution order -changes dynamically to support cooperative calls to :func:`super`. This -approach is known in some other multiple-inheritance languages as -call-next-method and is more powerful than the super call found in -single-inheritance languages. - -Dynamic ordering is necessary because all cases of multiple inheritance exhibit -one or more diamond relationships (where at least one of the parent classes -can be accessed through multiple paths from the bottommost class). For example, -all classes inherit from :class:`object`, so any case of multiple inheritance -provides more than one path to reach :class:`object`. To keep the base classes -from being accessed more than once, the dynamic algorithm linearizes the search -order in a way that preserves the left-to-right ordering specified in each -class, that calls each parent only once, and that is monotonic (meaning that a -class can be subclassed without affecting the precedence order of its parents). -Taken together, these properties make it possible to design reliable and -extensible classes with multiple inheritance. For more detail, see -:ref:`python_2.3_mro`. - -In some cases multiple inheritance is not allowed; see :ref:`multiple-inheritance` -for details. + For examnple:: + + >>> class Animal: + ... def who_am_i(self): + ... print("I'm an Animal") + ... + >>> class Cat(Animal): + ... pass + ... + >>> class Dog(Animal): + ... def who_am_i(self): + ... print("I'm a Dog") + ... + >>> class CatDog(Cat, Dog): + ... pass + ... + >>> cat_dog = CatDog() + >>> cat_dog.who_am_i() + I'm a Dog + +In reality it is a little more complicated. Python uses the C3 method +resolution order (MRO). Each class has a built-in attribute +:attr:`~type.__mro__` that returns a tuple of classes that are considered +during method resolution:: + + >>> for cls in CatDog.__mro__: + ... print(cls) + ... + + + + + + +For more details, see :ref:`python_2.3_mro`. .. _tut-private: From c0112c7d5bf99de9ea9dd3080b4c4c4f6401ed2e Mon Sep 17 00:00:00 2001 From: Ievgen Kapinos Date: Sun, 12 Oct 2025 15:41:44 +0200 Subject: [PATCH 2/5] indenation --- Doc/tutorial/classes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 29149d5af856f7..33a1ad33deaf2b 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -640,7 +640,7 @@ multiple base classes looks like this:: For most purposes, in the simplest cases, you can think of the search for attributes inherited from a parent class as breadth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy. - For examnple:: +For examnple:: >>> class Animal: ... def who_am_i(self): From 1ee8c2a1e5e4ecd3e443c075c02ffb845b993324 Mon Sep 17 00:00:00 2001 From: Ievgen Kapinos Date: Sun, 12 Oct 2025 15:44:34 +0200 Subject: [PATCH 3/5] linter --- Doc/tutorial/classes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 33a1ad33deaf2b..6c5279dba9c94f 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -660,8 +660,8 @@ For examnple:: >>> cat_dog.who_am_i() I'm a Dog -In reality it is a little more complicated. Python uses the C3 method -resolution order (MRO). Each class has a built-in attribute +In reality it is a little more complicated. Python uses the C3 method +resolution order (MRO). Each class has a built-in attribute :attr:`~type.__mro__` that returns a tuple of classes that are considered during method resolution:: From f6d67c897e057f1bdf23e759f97e74806ea49852 Mon Sep 17 00:00:00 2001 From: Ievgen Kapinos Date: Sun, 12 Oct 2025 15:46:41 +0200 Subject: [PATCH 4/5] linter --- Doc/tutorial/classes.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 6c5279dba9c94f..5e4014157d573c 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -643,21 +643,21 @@ searching twice in the same class where there is an overlap in the hierarchy. For examnple:: >>> class Animal: - ... def who_am_i(self): + ... def whoami(self): ... print("I'm an Animal") ... >>> class Cat(Animal): ... pass - ... + ... >>> class Dog(Animal): - ... def who_am_i(self): + ... def whoami(self): ... print("I'm a Dog") - ... + ... >>> class CatDog(Cat, Dog): ... pass ... >>> cat_dog = CatDog() - >>> cat_dog.who_am_i() + >>> cat_dog.whoami() I'm a Dog In reality it is a little more complicated. Python uses the C3 method From 4dd71dceea44648c0bd73085b08404139954c7f5 Mon Sep 17 00:00:00 2001 From: Ievgen Kapinos Date: Sun, 12 Oct 2025 15:48:43 +0200 Subject: [PATCH 5/5] polishing --- Doc/tutorial/classes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 5e4014157d573c..df5f70ab6251d4 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -656,8 +656,8 @@ For examnple:: >>> class CatDog(Cat, Dog): ... pass ... - >>> cat_dog = CatDog() - >>> cat_dog.whoami() + >>> catdog = CatDog() + >>> catdog.whoami() I'm a Dog In reality it is a little more complicated. Python uses the C3 method