6. FuncMechanicsH.pdf - 6 How Modules and Functions Work Topics Modules and Functions More on Importing Call Frames Let\u2019s Talk About Modules Inside a

# 6. FuncMechanicsH.pdf - 6 How Modules and Functions Work...

This preview shows page 1 - 5 out of 12 pages.

1/22/2016 1 6. How Modules and Functions Work Topics: Modules and Functions More on Importing Call Frames Let’s Talk About Modules What Are They? M1.py A module is a .py file that contains Python code The name of the module is the name of the file. This is the module M1.py We draw a module as a folder with a black outline. A module may contain a single script. A script will be shown as a rectangle with a red border. Inside a Module Kepler.py : : r = 1 E = sqrt(6)*r : : A module may contain one or more function definitions. Functions will be shown as rectangles with green borders. Inside a Module SimpleMath.py sqrt cos sin A module may contain one or more data items. These are referred to as global variables. They should be treated as constants whose values are never changed. Data items will be shown as rectangles with blue borders. Inside a Module SimpleGraphics.py RED = [1.,0.,0.] BLUE = [0.,0.,1.] CYAN = [0.,1.,1.]
1/22/2016 2 A module may contain one or more data items and one or more functions. Inside a Module SimpleGraphics.py RED = [1.,0.,0.] CYAN = [0.,1.,1.] MakeWindow DrawRect A module may contain one or more data items and one or more functions and a script. But in this case, the script MUST be prefaced by if __name__ == __main__: Inside a Module M.py Let’s Talk About import What Does import Allow? import M2 M1.py It means that code inside M1.py can reference the data and functions inside M2.py M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] What Does import Allow? import M2 : M1.py A function in M1.py could have a line like a = M2.func2(x,M2.GlobVar1) M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] What Does import Allow? import M2 : M1.py The script in M1.py could have a line like a = M2.func1(M2.GlobVar1) M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0]
1/22/2016 3 One Way to Think About this... import M2 : : M1.py M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] i s like this… M1.py M2 GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] Module M1.py contains a folder called M2 . Need the “dot notation’’ to extract what is in M2 . What Does import* Allow? from M2 import* : M1.py A function in M1.py could have a line like a = func1(x,GlobalVar2) M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] No dot notation What Does import* Allow? from M2 import* : M1.py A script in M1.py could have a line like a = func2(x,GlobalVar2) M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] No dot notation One way to Think about this… from M2 import* : M1.py M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] i s like this… M1.py func1(x) func2(x,y) GlobVar2=[1,0,0] GlobVar1=3.14 It is as if GlobVar1 , GlobVar2 , func1 , and func2 were defined in M1.py
1/22/2016 4 “Specific” Importing from M2 import func2 : M1.py A script in M1.py could have a line like a = func2(3,4) M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] No dot notation “Specific” Importing from M2 import func2 : M1.py A script in M1.py could NOT have a line like a = func1(4) M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] No dot notation One way to think about this… from M2 import func2 : M1.py M2.py GlobVar1=3.14 func1(x) func2(x,y) GlobVar2=[1,0,0] is like this… M1.py

#### You've reached the end of your free preview.

Want to read all 12 pages?

• Fall '19
• Subroutine, Return statement