Skip to ContentGo to accessibility page

13.2 Attribute access

Learning objectives

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.

Checkpoint

Defining a simple subclass

Defining a simple subclass; ch 13, video 3

Concepts in Practice

Using simple subclasses

1.
Consider the Employee and Developer example above. What is the value of dev_1.e_id?
  1. 1
  2. 2
  3. Error
2.
Consider the following example. Line 13 executes and SubClass() 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()
  1. 2
  2. 10
  3. Error
3.
Consider the following example. Which instance attribute(s) does dc_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()
  1. feat_2
  2. feat_1 and feat_2
  3. 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.

Checkpoint

Defining __init__() in a subclass

Defining _init_() in a subclass; ch 13, video 4

Concepts in Practice

Accessing a subclass's attributes

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 of dev_1.dev_display()?
  1. Employee 2 hired in 2023
  2. Proficient in ['Python', 'C++', 'Java']
  3. Error
5.
What would be the output of emp_1.dev_display()?
  1. Employee 1 hired in 2023
  2. Proficient in ['Python', 'C++', 'Java']
  3. Error
6.
Suppose dev_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()?
  1. print(f"Employee {self.e_id} proficient in {self.lang_xp}")
  2. print(f"Employee {self.Employee.e_id} proficient in {self.lang_xp}")
  3. print(f"Employee 2 proficient in {self.lang_xp}")

Try It

Creating a subclass with an instance attribute

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
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.