1 / 6

Modules

Modules. Module. Collection of functions and global variables Stores in a single file Named with . py extension. Using Your Module. import <filename> Do not include extension . py in filename Access function in module: <filename>.< function_name >(< argument_list >)

gin
Download Presentation

Modules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modules

  2. Module • Collection of functions and global variables • Stores in a single file • Named with .py extension

  3. Using Your Module • import <filename> • Do not include extension .py in filename • Access function in module: • <filename>.<function_name>(<argument_list>) • Access global variable from module: • <filename>.<variable_name>

  4. Gotchas • Any statements that are not in a function in the imported module will be executed when the module is imported. • Be careful with circular imports • A imports B and B imports A. • Will mostly likely result in an error

  5. Dividing Code over Modules • A module contains functions that are closely related • random • grid • math • Etc • Avoid global variables in modules • Anyone can change the value of the variable!

  6. Alternate Forms for Import • Standard way to import: • import <filename> • x = random.randint(1,3) • Importing a function (or variable) • from random import randint • x = randint(1,3) • Importing entire module avoids naming conflicts • Importing function (or variable) makes code that uses function simpler to read

More Related