func1(x)
func2(x,y)
GlobVar2=[1,0,0]
is like this…
M1.py
func2(x,y)
It is as if
func2
was defined
in
M1.py
Using Stuff Within a Module
M.py
The functions and
global variables
in
M.py
can be
used
throughout
M.py
without the dot
notation
There are rules about when a
module
M2.py
can be imported
by a module
M1.py

2/18/2016
5
Does this Always Work?
Yes,
if
M2.py is
a module that is part
of the CS 1110 Python installation, e.g.,
math
numpy
urllib2 string
scipy
PIL
random timeit
.
import M2
:
M1.py
Does this Always Work?
No UNLESS
M1.py
and
M2.py
are each
in the “current working directory
”.
.
import M2
:
M1.py
Comments on
“Launching” a Python Computation
In what follows, this
will be how we indicate
what’s in the “current
working directory”
And this will mean we
are in the command shell
and in the “current
working directory”
cwd >
import M2
M1.py
cwd >
python M1.py
Result: the script in
M1.py
is executed.
M2.py
import M2
M1.py
M2.py
cwd >
python M1.py
Result: the script in
M1.py
is executed. The script in
M2.py
is not executed
import M2
M1.py
M2.py
cwd >
python M1.py
Result: Nothing happens because
there is no
script in
M1.py
to execute.

6
M2
M1
M1.py
We now focus our attention on the mechanics behind function calls.Somewhat Like Plugging into a Formula For the simple kind of fruitful functions that we have been considering, there is a substitution process. Exactly how does it work?

2/18/2016
7
We Use This Example…
def T(s):
“““ Returns as
int the number of minutes
from 12:00 to the time specified by s.
PreC: s is a length-5 string of the form
‘
hh:mm
’ that specifies the time.”””
h = int(s[:2])
m = int(s[3:])
if h<12:
z = 60*h+m
else:
z = m
return z
A Script
s1 = ‘11:15’
s2 = ‘12:05’
x = T(s1)
y = T(s2)
if y>=x:
numMin = y-x
else:
numMin = (y+720)-x
This assigns to
numMin
the number of minutes in a
class that starts at the time specified by
s1
and
ends at the time specified by
s2
.
function call
function call
A Script
s1 = ‘11:15’
s2 = ‘12:05’
x = T(s1)
y = T(s2)
if y>=x:
numMin = y-x
else:
numMin = (y+720)-x
print numMin
Prints the number of minutes in a class that starts
at the time specified by
s1
and ends at the time
specified by
s2
.
Let us step through its execution.
s1 = ‘11:15’
s2 = ‘12:05’
x = T(s1)
y = T(s2)
if y>=x:
numMin = y-x
else:
numMin = (y+720)-x
print numMin
1
s1
s2
x
y
numMin
This box is called
Global Space. It includes
all the variables associated
with the script.
The red dot indicates
the next thing to do in
the script.
s1 = ‘11:15’
s2 = ‘12:05’
x = T(s1)
y = T(s2)
if y>=x:
numMin = y-x
else:
numMin = (y+720)-x
print numMin
2
s1
s2
x
y
numMin
‘11:15’
s1 = ‘11:15’
s2 = ‘12:05’

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

Want to read all 11 pages?

- Spring '08
- DAVIDGRIES
- Subroutine, Return statement, Control flow, nu mMi, M1.py