From ca7f6ac05cecd7858bc9ba0e0b5d3edb044e7f84 Mon Sep 17 00:00:00 2001 From: Steve Oney Date: Mon, 31 Jul 2023 13:24:04 -0400 Subject: [PATCH 1/3] Add chapter on multiple inheritance --- .../Inheritance/WPMultipleInheritance.rst | 24 +++++++++++++++++++ _sources/Inheritance/toctree.rst | 1 + 2 files changed, 25 insertions(+) create mode 100644 _sources/Inheritance/WPMultipleInheritance.rst diff --git a/_sources/Inheritance/WPMultipleInheritance.rst b/_sources/Inheritance/WPMultipleInheritance.rst new file mode 100644 index 00000000..50ae0091 --- /dev/null +++ b/_sources/Inheritance/WPMultipleInheritance.rst @@ -0,0 +1,24 @@ +👩‍💻 Multiple inheritance +========================== + +In Python, a class can inherit from multiple classes. This is called **multiple inheritance**. Multiple inheritance can be useful when you want to create a class that is a combination of multiple classes. For example, suppose we have a class ``Swimmer`` (which represents all of the aspects of a character that can swim) and a class ``Flyer`` (for all of the aspects of a character that relate to flying). We can create a class ``SwimmerFlyer`` that inherits from both ``Swimmer`` and ``Flyer`` by putting both these class names in parentheses: ``class SwimmerFlyer(Swimmer, Flyer)``. This class will have all the methods and attributes of both ``Swimmer`` and ``Flyer``: + +.. activecode:: multiple_inheritance_example + :nocanvas: + + class Swimmer: + def swim(self): + print("Swimming!") + + class Flyer: + def fly(self): + print("Flying!") + + class SwimmerFlyer(Swimmer, Flyer): + pass + + sf = SwimmerFlyer() + sf.swim() # Swimming! + sf.fly() # Flying! + +Multiple inheritance can improve our ability to re-use code and classes. It can be particularly useful if the classes represent "features" that we can selectively apply to subclasses. However, **it's generally a good rule to avoid multiple inheritance unless it provides a clear and significant benefit**. Always consider simpler alternatives, such as composition (using an instance of one class as an instance variable inside of another class) or single inheritance, before turning to multiple inheritance. \ No newline at end of file diff --git a/_sources/Inheritance/toctree.rst b/_sources/Inheritance/toctree.rst index 0f5beb4a..fbf40346 100644 --- a/_sources/Inheritance/toctree.rst +++ b/_sources/Inheritance/toctree.rst @@ -10,6 +10,7 @@ Inheritance OverrideMethods.rst InvokingSuperMethods.rst TamagotchiRevisited.rst + WPMultipleInheritance.rst Exercises.rst ChapterAssessment.rst chapterProject.rst From 753e9238557cc23152b7e339c4cacec1a9b49c8c Mon Sep 17 00:00:00 2001 From: Paul Resnick Date: Wed, 9 Aug 2023 16:24:32 -0400 Subject: [PATCH 2/3] small improvements to multiple inheritance --- _sources/Inheritance/MultipleInheritance.rst | 33 ++++++++++++++++++++ _sources/Inheritance/toctree.rst | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 _sources/Inheritance/MultipleInheritance.rst diff --git a/_sources/Inheritance/MultipleInheritance.rst b/_sources/Inheritance/MultipleInheritance.rst new file mode 100644 index 00000000..67927fec --- /dev/null +++ b/_sources/Inheritance/MultipleInheritance.rst @@ -0,0 +1,33 @@ +.. Copyright (C) Steve Oney. Permission is granted to copy, distribute + and/or modify this document under the terms of the GNU Free Documentation + License, Version 1.3 or any later version published by the Free Software + Foundation; with Invariant Sections being Forward, Prefaces, and + Contributor List, no Front-Cover Texts, and no Back-Cover Texts. A copy of + the license is included in the section entitled "GNU Free Documentation + License". + + +Multiple inheritance +======================== + +In Python, a class can inherit from more than one parent class. This is called **multiple inheritance**. Multiple inheritance can be useful when you want to create a class that is a combination of multiple classes. For example, suppose we have a class ``Swimmer`` (which represents all of the aspects of a character that can swim) and a class ``Flyer`` (for all of the aspects of a character that relate to flying). We can create a class ``Goose`` that inherits from both ``Swimmer`` and ``Flyer`` by putting both these class names in parentheses: ``class Goose(Swimmer, Flyer)``. This class will have all the methods and attributes of both ``Swimmer`` and ``Flyer``: + +.. activecode:: multiple_inheritance_example + :nocanvas: + + class Swimmer: + def swim(self): + print("Swimming!") + + class Flyer: + def fly(self): + print("Flying!") + + class Goose(Swimmer, Flyer): + pass + + my_goose = Goose() + my_goose.swim() # Swimming! + my_goose.fly() # Flying! + +Multiple inheritance can improve our ability to re-use code and classes. It can be particularly useful if the classes represent "features" that we can selectively apply to subclasses. However, **it's generally a good rule to avoid multiple inheritance unless it provides a clear and significant benefit**. Always consider simpler alternatives, such as composition (using an instance of one class as an instance variable inside of another class) or single inheritance, before turning to multiple inheritance. \ No newline at end of file diff --git a/_sources/Inheritance/toctree.rst b/_sources/Inheritance/toctree.rst index fbf40346..875e9f4f 100644 --- a/_sources/Inheritance/toctree.rst +++ b/_sources/Inheritance/toctree.rst @@ -9,8 +9,8 @@ Inheritance inheritVarsAndMethods.rst OverrideMethods.rst InvokingSuperMethods.rst + MultipleInheritance.rst TamagotchiRevisited.rst - WPMultipleInheritance.rst Exercises.rst ChapterAssessment.rst chapterProject.rst From 868441a7e7951666d6e152009d9269dc47449eb5 Mon Sep 17 00:00:00 2001 From: Paul Resnick Date: Wed, 9 Aug 2023 16:25:59 -0400 Subject: [PATCH 3/3] renamed file --- .../Inheritance/WPMultipleInheritance.rst | 24 ------------------- 1 file changed, 24 deletions(-) delete mode 100644 _sources/Inheritance/WPMultipleInheritance.rst diff --git a/_sources/Inheritance/WPMultipleInheritance.rst b/_sources/Inheritance/WPMultipleInheritance.rst deleted file mode 100644 index 50ae0091..00000000 --- a/_sources/Inheritance/WPMultipleInheritance.rst +++ /dev/null @@ -1,24 +0,0 @@ -👩‍💻 Multiple inheritance -========================== - -In Python, a class can inherit from multiple classes. This is called **multiple inheritance**. Multiple inheritance can be useful when you want to create a class that is a combination of multiple classes. For example, suppose we have a class ``Swimmer`` (which represents all of the aspects of a character that can swim) and a class ``Flyer`` (for all of the aspects of a character that relate to flying). We can create a class ``SwimmerFlyer`` that inherits from both ``Swimmer`` and ``Flyer`` by putting both these class names in parentheses: ``class SwimmerFlyer(Swimmer, Flyer)``. This class will have all the methods and attributes of both ``Swimmer`` and ``Flyer``: - -.. activecode:: multiple_inheritance_example - :nocanvas: - - class Swimmer: - def swim(self): - print("Swimming!") - - class Flyer: - def fly(self): - print("Flying!") - - class SwimmerFlyer(Swimmer, Flyer): - pass - - sf = SwimmerFlyer() - sf.swim() # Swimming! - sf.fly() # Flying! - -Multiple inheritance can improve our ability to re-use code and classes. It can be particularly useful if the classes represent "features" that we can selectively apply to subclasses. However, **it's generally a good rule to avoid multiple inheritance unless it provides a clear and significant benefit**. Always consider simpler alternatives, such as composition (using an instance of one class as an instance variable inside of another class) or single inheritance, before turning to multiple inheritance. \ No newline at end of file