Beginning Programming in Python
Fall 2019
Agenda
- Modules:
- Some useful modules
- Hierarchical namespaces
- Making your own modules
- The main() function
- PEP 8 (v. briefly)
- A revisit to debugging, now that we're writing longer programs:
- Looking at different error types (syntax, runtime, logical)
Modules
- A module is a file that contains Python code
- That file may contain functions that can be imported for your use
- In general, Python standard library provides loads of useful modules for all sorts of things
# From a user perspective, modules are variables, functions, objects etc.
import math
# This line "imports" the math module, so that we can refer to it
math.log10(100)
# Now we're calling a function from the math module to compute log_10(100)
Random module
Namespace & dot notation
-
To recap, the namespace is all identifiers available to a line of code
-
Namespaces are organized hierarchically into sub-pieces using modules, functions, and classes
-
If you want to use a function from another module you need to import it into the namespace of your code and use '.' notation
import math # Imports the math module into the current namespace
# The '.' syntax is a way of indicating membership
math.sqrt(2) # sqrt is a function that "belongs" to the math module
# (Later we'll see this notation reused with objects)
revisiting "import"
- If you want to use a function in a module, then that module/function must be imported first
from math import sqrt
sqrt(2.0) # Now sqrt is a just a function in the current program's name space,
# no dot notation required
from math import * # Import all functions from math
# But, this is generally a BAD IDEA, because you need to be sure
# this doesn't bring in things that will collide with other things
# used by the program
log(10)
# More useful is the "as" modifier
from math import sqrt as square_root # This imports the sqrt function from math
# but names it square_root. This is useful if you want to abbreviate a long function
# name, or if you want to import two separate things with the same name
square_root(2.0)
what's in a module?
- If a module is a part of your Python installation, then you can find out what it contains like this:
- But if a module is "famous" (like all the ones we will be using), then just Google it!
help('random')
writing your own module
-
You can write your own modules:
-
Create a file whose name is x.py, where x is the name of the module you want to create
-
Edit x.py to contain the functions you want
-
Create a new file y.py in the same directory and include "import x" at the top of y.py
-
The main function
-
You may write a program and then want to reuse some of the functions by importing them into another program.
-
The problem is that when you import a module it is executed.
-
Question: How do you stop the original program from running when you import it as a module?
-
Answer: By putting the logic for the program in a "main()", which is only called if the program is being run by the user, not imported as a module.
Main function
5 minutes break!
pep8: use style
-
It is easy to rush and write poorly-structured, hard-to-read code. However, generally, this proves a false-economy, resulting in longer debug cycles, a larger maintenance burden, and less code reuse.
-
Although many sins have nothing to do with the cosmetics of the code, some can be fixed by adopting a consistent, sane set of coding conventions. Python did this with Python Enhancement Proposal (PEP) 8
pep8: use style
- use 4 spaces (instead of tabs) for indentation
- limit line length to 78 characters
- when naming identifiers, use CamelCase for classes and lowercase_with_underscores for functions & variables
- place imports at the top of the file
- keep function definitions together
- use docstrings to document functions
- use two blank lines to separate function definitions from each other
- keep top-level statements, including function calls, together at the bottom of the program
Debugging revisited
- Syntax Error
- Runtime Error
- Semantic/Logical Error
Syntax error Example
runtime/semantic error
debug strategies
-
Use print statements dotted around the code to figure out what code is doing at specific points of time
-
Use a debugger - this allows you to step through execution, line-by-line, seeing what the program is up to at each step.
-
Write unit-tests for individual parts of the code
-
Use assert to check that expected properties are true
-
STARE HARD AT IT! Semantic errors will generally require you to question your program's logic.
debug Example
Lecture 10 challenge
Questions?
CSE 20 - Lecture 10
By Narges Norouzi
CSE 20 - Lecture 10
- 1,521