Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Import functions from a module using the from keyword.
  • Explain how to avoid a name collision when importing a module.

The from keyword

Specific functions in a module can be imported using the from keyword:

    from area import triangle, cylinder

These functions can be called directly, without referring to the module:

    print(triangle(1, 2))
    print(cylinder(3, 4))

Exploring further

As shown below, the from keyword can lead to confusing names.

Checkpoint

Importing functions

Concepts in Practice

The from keyword

1.
Which import statement would be needed before running print(sqrt(25))?
  1. from math import sqrt
  2. import sqrt from math
  3. import math
2.
How many variables are defined by the statement from math import sin, cos, tan?
  1. 0
  2. 3
  3. 4
3.
What error would occur if attempting to call a function that was not imported?
  1. ImportError
  2. NameError
  3. SyntaxError

Name collisions

Modules written by different programmers might use the same name for a function. A name collision occurs when a function is defined multiple times. If a function is defined more than once, the most recent definition is used:

    from area import cube

    def cube(x):    # Name collision (replaces the imported function)
      return x ** 3
    
    print(cube(2)) # Calls the local cube() function, not area.cube()

A programmer might not realize the cube function is defined twice because no error occurs when running the program. Name collisions are not considered errors and often lead to unexpected behavior.

Care should be taken to avoid name collisions. Selecting specific functions from a module to import reduces the memory footprint; however, importing a complete module can help to avoid collisions because a module.name format would be used. This is a tradeoff the programmer must consider.

Checkpoint

Module and function names

Concepts in Practice

Name collisions

4.
A program begins with from area import square, circle. What code causes a name collision?
  1. def area(phone):
      """Gets the area code of a phone number."""
    
  2. def circle(x, y, size):
      """Draws a circle centered at (x, y)."""
    
  3. def is_square(length, width):
      """Returns True if length and width are equal."""
    
5.
A program begins with import area. What code causes a name collision?
  1. area = 51
    
  2. import cylinder from volume
    
  3. def cube(size):
      """Generates a "size X size" rubik's cube."""
    
6.
Which line will cause an error?
1
def hello():
2
  print("Hello!")
3
4
def hello(name):
5
  print("Hello,", name)
6
7
hello()
8
hello("Chris")
  1. line 4
  2. line 7
  3. line 8

Exploring further

If a name is defined, imported, or assigned multiple times, Python uses the most recent definition. Other languages allow multiple functions to have the same name if the parameters are different. This feature, known as function overloading, is not part of the Python language.

Try It

Missing imports

Add the missing import statements 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

Party favors

The following program does not run correctly because of name collisions. Fix the program by modifying import statements, function calls, and variable assignments. The output should be:

    Bouncy ball area: 13
    Bouncy ball volume: 4
    Cone hat area: 227
    Cone hat volume: 209
Citation/Attribution

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 permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a 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 digital format, then you must 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

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo 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.