Class 29 — Monday, November 1
Listing
To be, not to be — That is the question I ask – The answer, to be
Look both ways
Agenda
- Problem solve
- Consider parameter passing nuances
- Consider optional parameter nuances
- Show
if ( __name__ == '__main__' )
statement for aiding testing
To do
- Examine next homework module aid.py.
Pillow download
- Program test-pillow.py
Parameter passing downloads
- Program up.py
- Module date.py
- Program my.py
- Module gosh.py
Optional parameters downloads
- Program alma.py
- Module mater.py
Module self-testing downloads
- Module im_a_module.py
- Tester im_a_tester_program.py
Remembering the past
# program cautionary.py | # module tale.py
import tale | def f( x, y ) :
city1 = 'London' | rmbr = x
city2 = 'Paris' | x = y
tale.f( city1, city2 ) | y = rmbr
print( city1, city2 ) | return
- We know from a prior class that it prints
London Paris
Puzzle me this
# program my.py | # module gosh.py
import gosh | def f( x ) :
nbrs = [ 3, 1, 4 ] | x = [ 0, 0, 0 ]
gosh.f( nbrs ) | return
print( nbrs ) |
- Does the above code print
[ 3, 1, 4 ]
- Or does the it print
[ 0, 0, 0 ]
Now what's happening
# program up.py | # module date.py
import date | def f( x ) :
nbrs = [ 3, 1, 4 ] | n = len( x )
date.f( nbrs ) | for i in range( 0, n ) :
print( nbrs ) | x[ i ] = 0
| return
- Does the above code print
[ 3, 1, 4 ]
- Or does the it print
[ 0, 0, 0 ]
Optional parameters
- Python supports functions having optional parameters
print( "hurry", end=" " )
print( "up", end=" " )
Produces
hurry up
- Module
alma.py
demonstrates a functionchant()
with two optional parameters.
def chant( msg='Wahoo Wah', n=3 ) :
for i in range( 0, n ) :
print( msg )
return
- program
mater.py
demonstrates uses ofchant()
alma.chant()
...
alma.chant( msg='Hip hip hooray!' )
...
alma.chant( n=5 )
...
alma.chant( 'We are CS 1112', 1 )
- A run of program
mater.py
produces output.
chanting with no parameters
Wahoo Wah
Wahoo Wah
Wahoo Wah
chanting with message parameter set
Hip hip hooray!
Hip hip hooray!
Hip hip hooray!
chanting with number of repetitions set
Wahoo Wah
Wahoo Wah
Wahoo Wah
Wahoo Wah
Wahoo Wah
chanting with arguments for message and repetition parameters
We are CS 1112
Tester statement if ( __name__ == '__main__' )
- Along with some built-in functions, Python provides each Python file with a built-in variable
__name__
.
- For a Python file being imported by another Python file, the variable is set to the name of imported file.
- For a Python file being run as a program, the variable is set to
'__main__'
- With this
if
statement, a module can provide self-testing if run as a program.
- Why do you care about this functionality?
- Such testing will also be part of the modules for ongoing homework and future tests
- Homework module
aid.py
uses theif ( __name__ == '__main__' )
statement to support testing usingabet.py
.
if ( __name__ == '__main__' ): # __name__ is a built in python variable.
import abet # it is set to the string '__main__' if
# you are running the file as a program
# it is set to the name of the file if
# the file is being imported
abet.test_rotate()
abet.test_rotate_k_times()
abet.test_common()
Module for next class aid.py — homework 24
Function rotate( x )
- Updates list
x
by moving the last element of listx
(if any) to the beginning of listx
. This type of list transformation is often called a circular shift.
- In carrying out its action, the function modifies the contents of the list
x
in place (e.g., by list-modification methods likepop()
,append()
, andinsert()
, or by directly modifying individual elements of listx
.
- The function returns
None
.
Function rotate_k_times( x, k )
- Updates list
x
by performingk
circular shifts.
- In carrying out its action, the function modifies the contents of the list
x
in place.
- The function returns
None
Function common( x, y )
- Returns a new list whose elements are those elements in
x
that are also iny
. The ordering of elements in the return list should reflect their ordering inx
.
- The function does not modify parameters
x
andy
in any way.
Slide show