Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Python – Part IV Dr. Nancy Warter-Perez May 19, 2005.

Similar presentations


Presentation on theme: "An Introduction to Python – Part IV Dr. Nancy Warter-Perez May 19, 2005."— Presentation transcript:

1 An Introduction to Python – Part IV Dr. Nancy Warter-Perez May 19, 2005

2 5/19/05Introduction to Python – Part IV2 Overview Scopes Modules Doc Strings Debugging

3 5/19/05Introduction to Python – Part IV3 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: x = 5 def fnc(): x = 2 print x, fnc() print x >>> 2 5 Ex 2: x = 5 def fnc(): global x x = 2 print x, fnc() print x >>> 2 2

4 5/19/05Introduction to Python – Part IV4 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

5 5/19/05Introduction to Python – Part IV5 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 5/19/05Introduction to Python – Part IV6 Python Documentation Sources #comments In-file documentation The dir function Lists of attributes available on objects Docstrings:__doc__In-file documentation attached to objects

7 5/19/05Introduction to Python – Part IV7 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

8 5/19/05Introduction to Python – Part IV8 Debugging Can use print statements to “manually” debug Can use debugger in PythonWin In Class Example

9 5/19/05Introduction to Python – Part IV9 Project Teams and Presentation Assignments Base Project (Global Alignment): Extension 1 (Local Alignment): Extension 2 (Ends-Free Global Alignment): Extension 3 (Affine Gap Penalty): Extension 4 (Database):


Download ppt "An Introduction to Python – Part IV Dr. Nancy Warter-Perez May 19, 2005."

Similar presentations


Ads by Google