Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

7.2: Importing Names

( \newcommand{\kernel}{\mathrm{null}\,}\)

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
  • import sqrt from math
  • 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."""
      
  • def circle(x, y, size):
      """Draws a circle centered at (x, y)."""
    
  • 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

    This page titled 7.2: Importing Names is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    • Was this article helpful?

    Support Center

    How can we help?