16.12: Chapter 13
- Page ID
- 117508
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)13.1 Inheritance basics
- a. A doughnut is a type of pastry, so
Doughnutinherits fromPastry. - b. A kitchen has a freezer, so a
Kitchenclass would contain aFreezerinstance. A has-a relationship is also called a composition. - b. The
Goalkeeperclass inherits from thePlayerclass. - b. The subclass name is followed by the superclass name in parentheses.
- a. Deriving
CarryOnfromLuggageis specified inCarryOn's class definition. Ex:class CarryOn(Luggage):. Instances of a subclass, likeCarryOn, are created like other classes and do not require the base class name. - c. A
SubClassinstance has access tofunc_1(), inherited fromSuperClass, as well as tofunc_2(), aSubClassmethod. - a. A
SuperClassinstance has access toSuperClass'sfunc_1()but doesn't have access to anything inSubClass, includingfunc_2().
13.2 Attribute access
- b.
dev_1is the secondEmployeecreated. Sodev_1is the second object to callEmployee.__init__(), anddev_1.e_idis assigned with2. - a.
SubClass's__init__()is not explicitly defined, soSuperClass's__init__()is executed. - b.
dc_1inheritsfeat_1andfeat_2fromSuperClass. - b.
dev_display()is aDevelopermethod that accessesdev_1's instance attributelang_xp. - c.
dev_display()is aDevelopermethod.emp_1is not aDeveloperinstance, soemp_1cannot accessdev_display(). - a. Instance attributes inherited from
Employee, the superclass, are accessed the same way as the subclassDeveloper's instance attributes.
13.3 Methods
- b. Overriding involves defining a subclass method that has the same name as the superclass method.
- b. The self parameter is missing from the overridden method's definition.
- c. The class definition doesn't indicate inheritance. The definition should be
class ContractTax(Tax):. - b.
Square's superclass isRectangle.Rectangle's__init__()is called on line 6. - a.
super().__init__(4)calls the__init__()ofRectangle's superclass,Polygon. Control moves to line 3, andnum_sidesis initialized with4. - b. The instance attribute side is initialized with
5. - b.
Rectangle's__init__()always callsPolygon's__init__()withnum_sides=4. - c. Polymorphism allows
print_type()to be overridden in subclasses ofPolygon. - a. The * operator can be used with integer operands as well as strings and other types.
13.4 Hierarchical inheritance
- a.
Class Ais the superclass forClass BandClass C. - b. Employee would be an appropriate superclass for
DeveloperandSalesRep.DeveloperandSalesRepare not directly related. - a.
B(2)resolves toB(2, 0), asb_attrhas a default value of0.a_attris assigned with2. - b.
c_insthas access toC's instance attribute,c_attr, as well the inherited attribute fromA,a_attr. - b.
D, which inherits fromB, sod_instinheritsb_attr.Balso inherits fromA, sod_instinheritsd_attr.
13.5 Multiple inheritance and mixin classes
- a.
1is the argument passed toC's__init__()fora_attr. - b. An argument is not passed to
C's__init__()forc_attr, soc_attris assigned with the default value,20. - a.
2maps tob_attrinB's__init__(). - a.
eat()only applies to carnivorous plants, whichPitcherandVenusFlyTraprepresent. - b.
eat()can be moved into a mixin class included inPitcher's andVenusFlyTrap's inheritance list. - b. Good practice is to have a mixin class name end in
"Mixin"to identify the class's purpose. The final implementation is:
class Plant:
def photosynth(self):
print("Photosynthesizing")
class CarnivMixin:
def eat(self):
print("Eating")
class Rose(Plant):
pass
class Pitcher(Plant, CarnivMixin):
pass
class VenusFlyTrap(Plant, CarnivMixin)
pass


