python_4 - Cal State LA
Download
Report
Transcript python_4 - Cal State LA
An Introduction to Python – Part IV
Dr. Nancy Warter-Perez
June 23, 2005
Overview
Creating 2-D Lists (already covered)
Scopes
Modules
Doc Strings
Debugging
Project Development Time
6/23/05
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 assign the value 5 to the element at the
2nd row and 3rd column of L
L[2][3] = 5
6/23/05
Introduction to Python – Part IV
3
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
6/23/05
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
4
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
Zero or more supplemental files known as
modules
6/23/05
Main control flow of program
Libraries of tools
Introduction to Python – Part IV
5
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')
6/23/05
Introduction to Python – Part IV
6
Python Documentation
Sources
6/23/05
#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
7
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
6/23/05
Introduction to Python – Part IV
8
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
6/23/05
Follow the flow of data
Follow the flow of control
Introduction to Python – Part IV
9
Debugging Tools/Methods
Can use print statements to “manually”
debug
Can use debugger in PythonWin
6/23/05
In Class Example
Introduction to Python – Part IV
10
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
6/23/05
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
11