- Cal State LA - Instructional Web Server
Download
Report
Transcript - Cal State LA - Instructional Web Server
An Introduction to Python – Part IV
Dr. Nancy Warter-Perez
Overview
Creating 2-D Lists (already covered)
Scopes
Modules
Doc Strings
Debugging
Project Development Time
Introduction to Python – Part IV
2
Creating 2-D Lists
To create a 2-D list L, with C columns and R rows
initialized to 0:
L = [[]]
#empty 2-Dlist
L = [[0 for col in range(C)] for row in range(R)]
To create a 2-D list initialized to 0 based on the length
of strings c and r:
L = [[0 for col in c] for row in r]
To assign the value 5 to the element at the 2nd row
and 3rd column of L
L[2][3] = 5
Introduction to Python – Part IV
3
Mutable and Immutable Objects
Immutable objects can not be changed
in place. They must be reassigned.
Immutable objects: simple variables,
tuples, strings
Ex:
>>>T = (1,2,3)
>>>T [2] = 5 # error!
>>>T = T[0:2] + (5,) # (1,2,5)
Note: there are no constant variables in
python
Introduction to Python – Part IV
4
Mutable objects
Mutable objects such as lists and
dictionaries can be changed in place
Ex:
>>> L = [1, 2, 3]
>>> L[2] = 88 # [1,2,88]
>>> D = {‘a’:1, ‘b’:2}
>>> D[‘c’] = 3 # {‘a’:1,’b’:2,’c’:3}
Introduction to Python – Part IV
5
Functions are passed by assignment
– Call by value or call by reference?
Immutable objects are similar to call by value.
Call by value will pass a copy of a variable a the
parameter and thus, will not change the variable
Example:
def fn(x):
x=x+1
y=5
fn(y)
print y
--- prints 5
Introduction to Python – Part IV
6
Functions are passed by assignment
– Call by value or call by reference?
Mutable objects are similar to call by reference
Call by reference – will pass the location of the
parameter and thus, will change the parameter
Example:
def fn(x):
x[1] = 77
y = [1, 2, 3]
fn(y)
print y
--- prints [1, 77, 3]
Introduction to Python – Part IV
7
Scopes
Scopes divine the
“visibility” of a variable
Variables defined outside
of a function are visible
to all of the functions
within a module (file)
Variables defined within
a function are local to
that function
To make a variable that
is defined within a
function global, use the
global keyword
Ex 1:
Ex 2:
x=5
x=5
def fnc():
def fnc():
x=2
global x
print x,
x=2
fnc()
print x,
print x
fnc()
>>> 2 5
print x
Introduction to Python – Part IV
>>> 2 2
8
Modules
Why use?
Code reuse
System namespace partitioning (avoid name
clashes)
Implementing shared services or data
How to structure a Program
One top-level file
Main control flow of program
Zero or more supplemental files known as
modules
Libraries of tools
Introduction to Python – Part IV
9
Modules - Import
Import – used to gain access to tools in
modules
Ex:
contents of file b.py
def spam(text):
print text, 'spam'
contents of file a.py
import b
b.spam('gumby')
Introduction to Python – Part IV
10
Using Global Variables with
Modules
Contents of aa.py
y=5
def x():
global y
y=y+1
def z():
global y
y=y+1
return y
Contents of bb.py
import aa
aa.x()
print aa.z()
print aa.y #ref global var
Output after running bb:
7
7
Introduction to Python – Part IV
11
Import happens only once!
Imports load and run a module’s code only the first time
Later imports use the already loaded module object
So, if you make changes to a module that you’ve already
imported, make sure to reload the module in the
interactive window
Ex:
After making changes to module aa (previous slide)
>>> reload (aa) #reload module aa
Introduction to Python – Part IV
12
Python Documentation
Sources
#comments
The dir function
In-file documentation
Lists of attributes
available on objects
Docstrings:__doc__ In-file documentation
attached to objects
Introduction to Python – Part IV
13
Dir and DocString Example
Ex: b.py
# Internal comment
"""Module Docstring comment """
def fn():
"""Function Docstring comment """
>>> import b
>>> dir(b)
['__builtins__', '__doc__', '__file__', '__name__', 'fn']
>>> print b.__doc__
Module Docstring comment
>>> print b.fn.__doc__
Function Doctring comment
Introduction to Python – Part IV
14
Debugging
Two types of bugs
Syntax errors – “easy” to find
Logical errors – harder to find
Can be a problem in your algorithm
Can be a problem in your coding
Debugging a program is like solving a puzzle
Must first understand what your program is
Logically trace what is happening in your program
supposed to do
What do you expect to happen versus what happened
Follow the flow of data
Follow the flow of control
Introduction to Python – Part IV
15
Debugging Tools/Methods
Can use print statements to “manually”
debug
Can use debugger in PythonWin
In Class Example
Introduction to Python – Part IV
16
Workshop
Separate your LCS functions into a different
module
Import the module and call the functions
from within your while loop (continually
prompting the user if they want to continue)
Trace through your program
Use print statements to view the score and trace
back matrices as they are formed
Using the debugger to view the score and trace
back matrices
Introduction to Python – Part IV
17