Skip to ContentGo to accessibility page
Introduction to Python Programming

11.5 Using modules with classes

Introduction to Python Programming11.5 Using modules with classes

11.5 Using modules with classes

Learning objectives

By the end of this section you should be able to

  • Construct an import statement that selectively imports classes.
  • Create an alias to import a module.

Importing classes from modules

Import statements, as discussed in the Modules chapter, allow code from other files, including classes, to be imported into a program. Accessing an imported class depends on whether the whole module is imported or only selected parts.

Multiple classes can be grouped in a module. For good organization, classes should be grouped in a module only if the grouping enables module reuse, as a key benefit of modules is reusability.

Checkpoint

Importing classes from files

Importing classes from files; ch 11, video 10

Concepts in Practice

Importing classes

1.
Consider the example above. How many classes are imported in the second main.py?
  1. 1
  2. 2
  3. 3
2.
Which line imports only the Level class from character?
  1. import Level from character
  2. from character import Level
  3. import Level
3.
How many classes can be in a module?
  1. none
  2. only 1
  3. any number

Using aliases

Aliasing allows the programmer to use an alternative name for imported items. Ex: import triangle as tri allows the program to refer to the triangle module as tri. Aliasing is useful to avoid name collisions but should be used carefully to avoid confusion.

Example 11.3

Using aliasing to import character

character.py

    class Level:
      def __init__(self, level=1):
        self.level = level
        ...
      def level_up(self):
        ...
    class XP:
      def __init__(self, XP=0):
        self.XP = XP
        ...
      def add_xp(self, num):
        ...

main.py

    import character as c
    
    bard_level = c.Level(1)
    bard_XP = c.XP(0)
    bard_XP.add_xp(300)
    bard_level.level_up()
    ...

Concepts in Practice

Using an alias

Consider the example above. Suppose a program imports the character module using import character as game_char.

4.
How would the program create a Level instance?
  1. rogue_level = character.Level()
  2. rogue_level = game_char.Level()
  3. rogue_level = character.game_char.Level()
5.
Which code creates a name collision?
  1. character = 'Ranger'
  2. Game_char = 'Ranger'
  3. game_char = 'Ranger'

Try It

Missing import statement

Add the missing import statement to the top of the file. Do not make any changes to the rest of the code. In the end, the program should run without errors.

Try It

Missing class import statement

Add the missing class import statement to the top of the file. Import only a class, not a full module. Do not make any changes to the rest of the code. In the end, the program should run without errors.

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.