16.10: Chapter 11
- Page ID
- 117506
\( \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}\)11.1 Object-oriented programming basics
- b. Ellis's username represents the post creator.
- b. An object models a real-world entity with fields and procedures.
- a. A procedure, such as
Change venue, can make changes to the field and protect the field from inappropriate access. - b. Limiting data access prevents accidental changes and makes relationships between objects clearer.
- b. Correct abstraction makes only necessary information visible. An overheated engine can cause irreparable damage, so an indicator is necessary for the driver.
- a. Showing only the necessary information reduces clutter and improves the overall experience.
11.2 Classes and instances
- a.
Catfollows the class keyword at the top of the class definition and is the name of the class. - c.
pet_1is an instance of theCatclass created by callingCat(). The other instance ispet_2. - a.
breedis an instance attribute definedin __init__(). The other instance attributes arenameandage. - c.
CapWordsstyle, with all words capitalized and no spaces between words, is recommended for class names. "Pet" and "cat" are separate words, so each is capitalized and one directly follows the other. - b. Two instances,
room_1androom_3, are created by callingRectangle(), which calls the constructor__init__(). - b.
room_1is the instance that called the methodarea(), soselfrepresents the specific instance. - a. Line 3 is in
__init__(), which initializes length and the other instance attributes for each new instance. - c.
__init__()has one required parameter, conventionally calledself, which allows the constructor to access the instance. - b.
order_idis an instance attribute defined in__init__(). The other instance attribute iscup_size. - b. Changing
order_1.cup_sizedoes not affectorder_3.cup_size. order_1.cup_sizechanges to8andorder_3.cup_sizeis still16. - a.
order_1,order_2, andorder_3all share the class attributeCoffeeOrder.loc. Changinglocaffects all instances, soCaffeine Cafeis printed as the location. - b. Trying to access a class attribute using an instance is unsafe. In
__init__(), trying to access the class attribute creates a new instance attribute calledcls_id. Though possible, programmers should avoid using the same name for class attributes and instance attributes to avoid confusion.
11.3 Instance methods
- b. Removing the default parameter value makes
bpa required parameter. - b.
selfis the first parameter in__init__()definition, not an explicit argument.selfshould not be included in the call. - c. The second definition overwrites the first as multiple explicit definitions of
__init__()aren't allowed.__init__()requires all parameters, so the first two__init__()calls forpatient_1andpatient_2are incorrect. - c.
order_1is originally a coffee without milk or sugar, but the call tochange()adds sugar to the order by assigningorder_1.with_sugarwithTrue. - c. An instance method's first parameter indicates the method can change an instance.
change()allows a change in whether a coffee order has milk or sugar. - b. Unindenting
print_order()would produce an error. An instance method must be defined in the class.
11.4 Overloading operators
- c.
__add__()is a magic method, indicated by the double underscores at the start and end of the method name. - b. Magic methods are designed to be invoked internally to perform under-the-hood actions to simplify tasks for the user.
- c.
__init__()is defined by the programmer to initialize instance attributes.__str__()is defined by the programmer to customize anEngineerinstance's string representation. - a. A programmer can overload an operator to customize the built-in operator's function.
- b. The addition operation maps the left operand,
acct_a, to the parameterselfand the right operand,acct_b, to the parameterother. - c. - is a binary operator, so
__sub__()has two parameters. The parameters'xandyattributes are subtracted from each other, and the resulting values are made into a newPair. - a. <, >, and == are overloaded with
__lt__(),__gt__(), and__eq__(), respectively. - b.
__le__()is the magic method for the <= operator.__le__()takes two parameters for the left and right operand, and returns the correct comparison of the two amount values. - b.
__gt__()overloads the > operator, which makes a comparison and returnsTrueorFalse.
11.5 Using modules with classes
- b.
LevelandXPare classes imported from the character module into the program. Any other class in the character module is not imported. - b. A single class or multiple classes can be imported from a module using the class
name(s). - c. The number of classes in a module is unlimited, but classes should be grouped in modules to maximize reusability.
- b. The character module is aliased as
game_char, so character is not defined in the program.game_charis used in place of character. - c.
game_charreferred to the imported module, but assigninggame_charreplaces the imported module with the string'Ranger'.game_charcan no longer be used to access classes.


