Class 29 — Monday April 12
Moving on
Working Together – On a cautionary tale – New understandings
Look both ways
Parameter passing downloads
- Program cautionary.py
- Module tale.py
- Program up.py
- Module date.py
- Program alma.py
- Module mater.py
Module self-testing downloads
- Module im_a_module.py
- Tester im_a_tester_program.py
Next homework
- Module aid.py.
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.
What the heck happens (PDF, slide show)
# 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
- Does the above code print
London Paris
- Or does the it print
Paris London
Now what the heck happens
# 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 ]
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 25
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
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |