13.2: Attribute Access
- Page ID
- 117607
\( \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}\)By the end of this section you should be able to
- Implement a subclass that accesses inherited attributes from the superclass.
- Write a subclass's
__init__()that inherits superclass instance attributes and creates new instance attributes.
Creating a simple subclass
Subclasses have access to the attributes inherited from the superclass. When the subclass's __init__() isn't explicitly defined, the superclass's __init__() method is called. Accessing both types of attributes uses the same syntax.
1.
Consider theEmployee and Developer example above. What is the value of dev_1.e_id?
-
1
2Error2.
Consider the following example. Line 13 executes andSubClass() is called. Which line does control flow move to?
|
1
|
class SuperClass: |
|
2
|
def __init__(self): |
|
3
|
self.feat_1 = 1 |
|
4
|
self.feat_2 = "" |
|
5
|
|
|
6
|
def bc_display(self): |
|
7
|
print(f"Superclass: {self.feat_2}") |
|
8
|
|
|
9
|
class SubClass(SuperClass): |
|
10
|
def dc_display(self): |
|
11
|
print(f"Subclass: {self.feat_2}") |
|
12
|
|
|
13
|
dc_1 = SubClass() |
-
2
-
10
-
Error
3.
Consider the following example. Which instance attribute(s) doesdc_1 have?
class SuperClass:
def __init__(self):
self.feat_1 = 1
self.feat_2 = ""
def bc_display(self):
print(f"Superclass: {self.feat_2}")
class SubClass(SuperClass):
def dc_display(self):
print(f"Subclass: {self.feat_2}")
dc_1 = SubClass()
-
feat_2 -
feat_1andfeat_2 -
None
Using __init__() to create and inherit instance attributes
A programmer often wants a subclass to have new instance attributes as well as those inherited from the superclass. Explicitly defining a subclass's __init__() involves defining instance attributes and assigning instance attributes inherited from the superclass.
Consider the Employee and Developer example code:
class Employee:
count = 0
def __init__(self):
Employee.count += 1
self.e_id = Employee.count
self.hire_year = 2023
def emp_display(self):
print(f"Employee {self.e_id} hired in {self.hire_year}")
class Developer(Employee):
def __init__(self):
Employee.count += 1
self.e_id = Employee.count
self.hire_year = 2023
self.lang_xp = ["Python", "C++", "Java"]
def dev_display(self):
print(f"Proficient in {self.lang_xp}")
emp_1 = Employee()
dev_1 = Developer()
4.
What would be the output ofdev_1.dev_display()?
-
Employee 2 hired in 2023
Proficient in ['Python', 'C++', 'Java']Error5.
What would be the output ofemp_1.dev_display()?
-
Employee 1 hired in 2023 -
Proficient in ['Python', 'C++', 'Java'] -
Error
6.
Supposedev_display() should be modified to display the developer's ID along with their proficiencies. Ex: dev_1.dev_display() would output Employee 2 proficient in ['Python', 'C++', 'Java']. Which is the appropriate new print() call in dev_display()?
-
print(f"Employee {self.e_id} proficient in {self.lang_xp}") -
print(f"Employee {self.Employee.e_id} proficient in {self.lang_xp}") -
print(f"Employee 2 proficient in {self.lang_xp}")
Given a class Dessert, create a class, Cupcake, inherited from Dessert. Cupcake class methods:
__init__(self): initializes inherited instance attribute ingredients with ["butter", "sugar", "eggs", "flour"], and initializes instance attribute frosting with "buttercream"display(self): prints a cupcake's ingredients and frosting
Then call the display() method on a new Cupcake object. The output should match:
Made with ["butter", "sugar", "eggs", "flour"] and topped with buttercream frosting


