=filename pyperl.pdf
@outputFile pyperl.xml
@style style,Styles
@frame 0,0,782,612
@footer 0,5,787,25
@codeRatio 0.85
.slide
@title Python for [Perl] Programmers
.spacer
=height 40
.smalltitle
Aahz
.spacer
=height 20
.smalltitle
``aahz@pythoncraft.com``
.smalltitle
``http://pythoncraft.com/``
.spacer
=height 60
.smalltitle
Powered by PythonPoint
.spacer
=height 10
.smalltitle
``http://www.reportlab.com/``
.slide
=outlinelevel 1
@head Meta Tutorial
.bullet
I'm hearing-impaired
.indent
Please write questions if at all possible (also helps with my book)
.bullet
Two breaks
.indent
15 minutes each, class will resume promptly
.bullet
Slides and scripts on web
.indent
Resources at end of slideshow (including all URLs)
.slide
=outlinelevel 1
@head This Class Is
.bullet
Fast
.indent
First two sections double-fast
.bullet
Intermediate level
.indent
Skim or skip lots of basics
.indent
See on-line tutorial for intro
.bullet
Focused Tour / Survey
.indent
Based on common questions from ``comp.lang.python``
.slide
=outlinelevel 1
@head This Class Is Not
.bullet
Advocacy
.bullet
Bashing
.indent
"P/P" indicates compare/contrast
.spacer
=height 10
.indent
Perl and Python have same fundamental goal:
programmer productivity
.slide
=outlinelevel 1
@head First code: Python 2.2.1
.spacer
=height 10
.code
@include grep.py
.slide
=outlinelevel 1
@head Interactive Python
.bullet
Quick demo
.slide
@head Contents
.bullet
Background
.bullet
Core Language I
.indent
Quick start
.bullet
Core II
.indent
In-depth
.bullet
Library
.bullet
Resources
.slide
@head Background
.bullet
History
.bullet
Dev process
.bullet
Philosophy
.bullet
Documentation
.slide
=outlinelevel 1
@head History
.bullet
Started 1989 on Mac
.bullet
Based on ABC and Modula-3
.bullet
Interface to C
.indent
Platform-neutral except for C's roots to Unix
.slide
=outlinelevel 1
@head Dev History
.bullet
1.5.2 released 4/1999
.indent
Red Hat 7.x still defaults :-(
.bullet
2.0 released 10/2000
.indent
Move to SourceForge
.bullet
2.2.1 released 4/2002
.indent
Current release; some apps / extensions require 2.1.x
(notably Zope 2.4.x)
.bullet
2.3 by end of 2002
.indent
Small release, but busy developers
.slide
=outlinelevel 1
@head Platforms
.bullet
CPython
.indent
Reference implementation in C, portable to almost all platforms that
support ANSI C
.indent
This class, "python" == "CPython"
.bullet
Jython
.indent
Java-based implementation, very similar to CPython
.indent
Major differences are memory management and OS-based libraries
.indent
Not covered in this class
.slide
=outlinelevel 1
@head Dev Process
.bullet
Guido van Rossum
.indent
BDFL: Benevolent Dictator For Life
.bullet
PEPs
.indent
Python Enhancement Proposals
.indent
Covers both process and features
.spacer
=height 15
.indent
Examples:
.indent
PEP 3 describes how to handle bug reports
.indent
PEP 278 is Universal Newline Support
.indent
PEP 283 is plan/schedule for Python 2.3
.slide
=outlinelevel 1
@head Making Decisions
.bullet
Informal
.indent
Apache-style voting (-1, -0, +0, +1), but Guido wants
reasons more than votes
.bullet
BDFL Pronouncement
.indent
It's over
.bullet
Guido intuition
.indent
Guido (almost) always right
.indent
Explanations sometimes suboptimal
.slide
=outlinelevel 1
@head P/P: Philosophy
.bullet
Perl
.indent
TMTOWTDI:
.indent
There's More Than One Way To Do It
.bullet
Python
.indent
There's Only One Way
.slide
=outlinelevel 1
@head Zen of Python
.bullet
Tim Peters
.indent
Channeling Guido van Rossum
.bullet
``import this``
.indent
Python 2.1.2 or higher
.slide
=outlinelevel 1
@head 20 Pythonic Theses
.numlist
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
.slide
=outlinelevel 1
@head Why Whitespace?
.bullet
ABC
.indent
Research shows 3 or 4 spaces best
.bullet
Good code already indents
.indent
No brace wars
.slide
=outlinelevel 1
@head Guido Style
.bullet
PEP 8
.indent
Python Style Guide
.bullet
Readability
.indent
Follow rules when sensible
.indent
Use judgment for breaking rules
.slide
=outlinelevel 1
@head Style Summary
.bullet
4-space indent
.bullet
Never mix tabs/spaces
.indent
``python -t`` or ``python -tt``
.bullet
Punctuation
.indent
one space around ``=``, compare operators
.indent
space after comma, colon, semi-colon
.indent
no space for parens
.slide
=outlinelevel 1
@head Doc Tour
.bullet
Standard docs
.indent
Tutorial
.indent
Library Reference
.indent
Global Module Index
.indent
Language Reference
.indent
What's New
.bullet
Search
.slide
@head Core Language I
.bullet
Data
.bullet
Control structures
.bullet
Functions, Modules, Packages
.bullet
OOP
.bullet
Applications
.slide
=outlinelevel 1
@head Built-in Data Types
.bullet
``None``
.bullet
Numbers
.bullet
Sequences / Strings
.bullet
Dictionaries
.bullet
``True`` / ``False``
.slide
=outlinelevel 2
@head ``None``
.bullet
P/P: ``undef``
.spacer
=height 10
.code
$ perl -e "print undef;"
$ python -c "print None"
None
$ python
>>> None
>>> print None
None
.bullet
Set explicitly
.indent
Except falling off end of function
.slide
=outlinelevel 2
@head Numbers
.bullet
Integer
.indent
Platform C ``int``, usually 32 bits
.bullet
Long
.indent
P/P: Similar to ``Math::BigInt``, but integrated into language
.bullet
Float
.indent
Platform ``double``
.bullet
Complex
.indent
``0+1j`` -- two floats
.slide
=outlinelevel 2
@head int/long Integration
.bullet
``1`` is int
.bullet
``1L`` is long
.bullet
Python 2.2+ autoconversion
.indent
``2 ** 100`` => long
.indent
triggered on ``OverflowError``
.bullet
int disappearing
.indent
Sometime between Python 2.4 and 3.0
.slide
=outlinelevel 2
@head Float Fun
.bullet
Python doesn't hide
.spacer
=height 10
.code
>>> 1.1
1.1000000000000001
>>> print 1.1
1.1
>>> repr(1.1)
'1.1000000000000001'
>>> str(1.1)
'1.1'
.slide
=outlinelevel 1
@head Sequences
.bullet
Types
.indent
Strings
.indent
Tuples
.indent
Lists
.bullet
What's a sequence?
.indent
Array/vector; contiguous storage of items
.slide
=outlinelevel 2
@head Strings
.bullet
Delimiters
.indent
``'``, ``"``, ``'''``, ``"""``
.indent
``r'``, ``r"``, ``r'''``, ``r"""``
.indent
``u'``, ``u"``, ``u'''``, ``u"""``
.indent
``ur'``, ``ur"``, ``ur'''``, ``ur"""``
.bullet
No implicit interpolation
.indent
``"%s %s" % (foo, bar)``
.indent
More later in the Text Processing section
.slide
=outlinelevel 2
@head String examples
.spacer
=height 10
.code
"Double quotes don't muck with single quotes"
''
'''
This is
a multi-line
string
'''
>>> r"abc\"xyz" '\\'
'abc\\"xyz\\'
.slide
=outlinelevel 2
@head Lists
.bullet
Perl array
.bullet
Syntax:
.spacer
=height 5
.code
>>> l = [1, 2, 3]
>>> l.append(2)
>>> l.pop()
2
>>> l.insert(0, 'abc')
>>> l
['abc', 1, 2, 3]
>>> l = [None] * 1000
>>> l = [8, 5, 13, 1]
>>> # Note: sort is in-place
... l.sort()
>>> l
[1, 5, 8, 13]
.slide
=outlinelevel 2
@head P/P: Lists
.bullet
No interpolation
.spacer
=height 10
.code
>>> [1, "abc", [4, 5], (9, 8, 7)]
[1, 'abc', [4, 5], (9, 8, 7)]
.slide
=outlinelevel 2
@head Tuples
.spacer
=height 10
.normal
Immutable sequence of heterogeneous items
.spacer
=height 10
.normal
Constructor is comma, not parentheses:
.code
>>> 1,'s'
(1, 's')
>>> 'abc',
('abc',)
.spacer
=height 5
.normal
But use parens for clarity
.spacer
=height 20
.normal
Empty tuple does require parens:
.code
>>> a = ()
>>> a
()
>>> type(a)
>>> len(a)
0
.slide
=outlinelevel 2
@head Tuple Assignment
.bullet
Tuple LHS
.bullet
Must match number
.spacer
=height 10
.code
>>> a,b = 3,5
>>> b
5
>>> a,b = b,a
>>> a,b = 7,8,9
Traceback (most recent call last):
File "", line 1, in ?
ValueError: unpack tuple of wrong size
.slide
=outlinelevel 2
@head Indexes
.bullet
Zero-based
.bullet
Negative
.indent
Starts from end of sequence
.bullet
Out of bounds exception
.spacer
=height 10
.code
>>> s = "abcdef"
>>> s[1]
'b'
>>> s[-2]
'e'
>>> s[6]
Traceback (most recent call last):
File "", line 1, in ?
IndexError: string index out of range
.slide
=outlinelevel 2
@head Slices
.bullet
Slice between elements
.spacer
=height 10
.code
>>> s = "abcdef"
>>> s[0:2]
'ab'
>>> s[1:]
'bcdef'
>>> s[-2:]
'ef'
>>> s[:-3]
'abc'
>>> s[3:10]
'cdef'
>>> s[:3] + s[3:]
'abcdef'
.slide
=outlinelevel 2
@head Slice Assignment
.spacer
=height 10
.code
>>> L = [0,1,2,3]
>>> L[1:3] = ['a','b','c','d']
>>> L
[0, 'a', 'b', 'c', 'd', 3]
>>> L[:2] = []
>>> L
['b', 'c', 'd', 3]
.slide
=outlinelevel 1
@head Dictionaries
.bullet
Dict is Perl hash (mostly)
.indent
Also called "mapping" or "associative array"
.bullet
Syntax:
.code
d = {
'a': 1,
1: [11, 12, 13]
('bar', 5): "xyz"
}
.bullet
Lists cannot be key
.slide
=outlinelevel 1
@head Mutable vs. Immutable
.bullet
Immutable objects
.indent
Strings, numbers, tuples, some class instances, and combinations
.bullet
Mutable objects
.indent
Lists, dicts, most classes and class instances
.bullet
Why immutable?
.indent
Dict keys, space efficiency
.slide
=outlinelevel 2
@head Mutable Subtleties
.bullet
Lists embedded in tuples
.spacer
=height 10
.code
>>> a = (1, ['foo'], 's')
>>> a[1] = {'foo': 'bar'}
Traceback (most recent call last):
File "", line 1, in ?
TypeError: object doesn't support item assignment
>>> a[1].append('bar')
>>> a
(1, ['foo', 'bar'], 's')
.spacer
=height 5
.indent
Can't be dict key
.bullet
Classes
.indent
Cannot force class immutable; it's an implied contract in code and
documentation
.slide
=outlinelevel 2
@head Tuples vs. Lists
.bullet
Which to use?
.indent
Convention enforced by Guido:
.indent
heterogeneous for tuples (light-weight struct) and homogeneous for lists
.spacer
=height 15
.indent
Other than hetero/homo, use lists except when you need immutability or
space efficiency -- list methods make your life easier
.indent
(As with the rest of the Style Guide, use your judgment)
.slide
=outlinelevel 1
@head Data Constructors
.bullet
Constructors also converters
.bullet
Examples
.spacer
=height 10
.code
>>> str(478)
'478'
>>> int("AC", 16)
172
>>> tuple(['abc'])
('abc',)
>>> tuple('abc')
('a', 'b', 'c')
>>> dict( (['a','d'], (5,'foo'), ['x',1]) )
{'a': 'd', 'x': 1, 5: 'foo'}
.slide
=outlinelevel 1
@head ``True`` / ``False``
.bullet
Python 2.3
.indent
Comparisons will return ``True`` or ``False`` instead of
``1`` or ``0``
.bullet
Python 2.2.1
.indent
``True`` and ``False`` added as builtin singletons (like ``None``) to
make backporting from 2.3 easier, but no other changes made
.slide
=outlinelevel 1
@head Control Structures
.spacer
=height 10
.bullet
``if`` / ``while``
.indent
P/P: Same as Perl, modulo short-circuit and no assignment in expressions
.bullet
``for``
.indent
P/P: Equivalent to Perl's ``foreach``, sort of
.bullet
``try``
.indent
``try``/``except``
.indent
``try``/``finally``
.slide
=outlinelevel 2
@head ``if``
.spacer
=height 10
.code
if :
...
elif :
...
else:
...
.slide
=outlinelevel 2
@head ``if`` Example
.spacer
=height 10
.code
if line:
if line.startswith('foo'):
foo(line)
elif line.startswith('bar'):
bar(line)
else:
process(line)
else:
cleanup()
.slide
=outlinelevel 2
@head ``while``
.spacer
=height 10
.code
while :
...
[ continue / break ]
...
else:
...
.bullet
``else`` skipped on ``break``
.slide
=outlinelevel 2
@head ``while`` Example
.spacer
=height 10
.code
done = False
while not done:
result = getInfo()
if result == END_TOKEN:
done = True
.slide
=outlinelevel 2
@head ``for``
.spacer
=height 10
.code
for in :
...
[ continue / break ]
...
else:
...
.bullet
``else`` skipped on ``break``
.bullet
P/P: similar to ``foreach``
.indent
But can use iterators, which Perl doesn't have
.slide
=outlinelevel 2
@head ``for`` Examples
.spacer
=height 10
.code
# print numbers from 0 through 9
for i in range(10):
print i
L = ["foo", "bar", "", "xyz"]
for item in L:
if not item:
continue
process(item)
if item == "STOP":
break
else:
print "No breaks encountered"
.slide
=outlinelevel 2
@head ``for`` vs. strings
.spacer
=height 10
.code
>>> for c in "abc":
... print c
...
a
b
c
>>> for item in ('abc', ('def', 'xyz')):
... print item
...
abc
('def', 'xyz')
.slide
=outlinelevel 2
@head Exceptions
.bullet
Structured throw of control
.indent
Dynamic scope, not lexical (unlike variables)
.indent
Unwinds call chain
.bullet
Errors
.indent
Exceptions mostly used for errors, but also used as a control structure
(e.g. ``for`` loop terminates on ``StopIteration`` or ``IndexError``)
.bullet
User-defined exceptions
.indent
Inherit from ``Exception`` or subclass of ``Exception``
.slide
=outlinelevel 2
@head Exception Syntax
.bullet
``try``/``except``
.spacer
=height 10
.code
try:
...
except [, []]:
...
else:
...
.bullet
``try``/``finally``
.spacer
=height 10
.code
try:
...
finally:
...
.slide
=outlinelevel 2
@head Using Exceptions
.bullet
List specific exception
.bullet
Get more info
.indent
``sys.exc_info``
.indent
``traceback``
.slide
=outlinelevel 2
@head Exceptions Demo
.bullet
``exceptions.py``
.slide
=outlinelevel 1
@head Functions and Modules
.bullet
Functions
.bullet
Modules
.bullet
Packages
.slide
=outlinelevel 2
@head Functions
.bullet
Syntax
.spacer
=height 10
.code
def ([]:
[global ]
...
[return []]
.bullet
Calling func
.indent
Passed parameters must match param list
.bullet
``return`` not required
.indent
Function falling off end returns ``None``
.indent
Bare ``return`` also returns ``None``
.slide
=outlinelevel 2
@head Default Arguments
.spacer
=height 10
.code
def get_int(num_tries=3):
for i in range(num_tries):
print "Enter number: ",
num = raw_input()
try:
num = int(num)
return num
except ValueError:
pass
raise ValueError, "No valid integer input"
x = get_int()
y = get_int(1)
.bullet
Avoid mutables
.indent
Binding to parameter performed at compile time
.slide
=outlinelevel 2
@head Keyword Arguments
.bullet
Named parameter in call
.spacer
=height 10
.code
def SQLexec(statement, connection=DEFAULT,
commit=False)
connection.send(statement, commit)
SQLexec(q)
SQLexec(q, commit=True)
SQLexec(q, commit=False,
connection=getConnection())
SQLexec(q, getConnection(), True)
.slide
=outlinelevel 2
@head Variable Arguments
.bullet
``*args``
.indent
Sequence of args
.bullet
``**kwargs``
.indent
Dict of args
.indent
"kwargs" == "keyword args"
.slide
=outlinelevel 2
@head Tuples and Functions
.bullet
Need parens to pass tuple:
.indent
Compare
.indent
``f(1, 2)``
.indent
``f( (1,2) )``
.slide
=outlinelevel 2
@head Copy Semantics
.bullet
Immutables don't need copy
.bullet
Mutables must be copied
.indent
If you don't want them modified, that is
.slide
=outlinelevel 2
@head Copy Example
.spacer
=height 15
.normal
``list.sort()`` is an in-place operation; here we create a function that
returns the list
.spacer
=height 10
.code
>>> def sort(L):
... L.sort()
... return L
...
>>> L = [11, 7, 12, 5]
>>> sort(L[:])
[5, 7, 11, 12]
>>> L
[11, 7, 12, 5]
>>> sort(L)
[5, 7, 11, 12]
>>> L
[5, 7, 11, 12]
.spacer
=height 15
.normal
Notice how failing to make copy modifies original list
.slide
=outlinelevel 2
@head Functions Demo
.bullet
``func.py``
.slide
=outlinelevel 2
@head Modules
.bullet
Script is module
.bullet
Executed only once
.indent
Only on first import
.slide
=outlinelevel 2
@head ``import``
.bullet
Three forms
.spacer
=height 10
.code
import foo
from foo import bar
from foo import *
.bullet
``import *``
.indent
Pollutes namespace
.indent
Makes debugging difficult
.indent
Does not import names starting with "_"
.indent
If ``__all__`` defined, uses that list
.slide
=outlinelevel 2
@head ``import`` Demo
.bullet
``grep.py``
.slide
=outlinelevel 2
@head Packages
.bullet
Package is directory
.indent
``__init__.py``
.bullet
Example
.spacer
=height 5
.code
foo/
__init__.py
bar.py
baz.py
.bullet
Importing
.spacer
=height 10
.code
import foo
foo.baz.x()
from foo import bar
bar.y()
.slide
=outlinelevel 1
@head OOP
.bullet
Syntax
.bullet
``__init__()``
.bullet
Inheritance
.bullet
Class vs. instance
.slide
=outlinelevel 2
@head ``class`` Syntax
.spacer
=height 10
.code
class [()]:
...
[def __init__(self, []):
...
]
...
[def (self, []):
...
]
.bullet
Like module
.indent
Code at ``class`` scope gets executed once
.slide
=outlinelevel 2
@head ``__init__()``
.bullet
Called automatically
.indent
After instance is created
.bullet
Not constructor
.slide
=outlinelevel 2
@head Inheritance
.bullet
Dynamic
.indent
All inheritance in Python is resolved at run-time
.bullet
Multiple inheritance
.indent
Mix-in classes provide convenience methods
.slide
=outlinelevel 2
@head is-a vs. has-a
.bullet
Inheritance vs. composition
.indent
Use inheritance only when derived class is-a subset or extension of base
class, or to inherit behavior (i.e. mix-in). Otherwise, use
composition (make an instance of class you want to use an attribute).
For example, an ``Employee`` class should contain a ``person``
attribute (not inherit from ``Person``), but ``SalariedEmployee`` could
inherit from ``Employee``.
.slide
=outlinelevel 2
@head Typing vs. Inheritance
.bullet
Protocol
.indent
Because Python only uses dynamic typing and late binding, classes do
not need to inherit to fit static type definitions. Just define the
appropriate methods and it works (except for a few operations that
require builtin types).
.slide
=outlinelevel 2
@head Class vs. Instance
.bullet
Instance attribute
.indent
Access with ``self``
.bullet
Class attribute
.indent
Access with class name
.slide
=outlinelevel 2
@head Private Data
.bullet
No private data
.indent
Introspective object system makes it impossible to have truly private
data. The closest Python comes is attributes with a leading double
underscore (e.g. ``__foo``), which get name-mangling -- but it's only
intended for namespace separation between base class and derived class;
a determined user can easily access the mangled name.
.indent
Instead, Python relies on convention (attributes with a single leading
underscore are considered private -- derived from ``import *``)
and documentation.
.slide
=outlinelevel 2
@head ``class`` demo
.bullet
``classes.py``
.slide
=outlinelevel 1
@head Applications
.bullet
Documenting apps
.bullet
Python files
.bullet
Distributing apps
.bullet
Testing
.bullet
Warnings
.slide
=outlinelevel 2
@head Documenting Apps
.bullet
Docstring
.indent
String: first non-comment line of module, class, or function/method
.indent
Can be multi-line string (and mostly should be)
.indent
Docstring format: PEP 257
.bullet
Docstrings vs. comments
.indent
Docstrings are accessible outside the source file; comments should
only be used for information of interest to someone reading the source
(i.e. implementation details)
.slide
=outlinelevel 2
@head Python Files
.bullet
``.py``
.indent
Source files
.bullet
``.pyc``
.indent
Compiled bytecode
.bullet
``.pyo``
.indent
Optimized bytecode
.bullet
``.pyd``
.indent
C Extensions
.slide
=outlinelevel 2
@head ``.pyo``
.bullet
``python -O``
.indent
Or ``PYTHONOPTIMIZE`` env var
.indent
Discards some bytecodes, including
.indent
``assert``
.indent
``if 0:``
.indent
``if __debug__:``
.bullet
``python -OO``
.indent
Discards docstrings
.slide
=outlinelevel 2
@head Distributing Apps
.bullet
``Tools/freeze``
.bullet
Installer
.bullet
py2exe
.indent
Uses ``distutils``
.indent
Windows-only
.slide
=outlinelevel 2
@head Testing
.bullet
Test first, then code
.bullet
``doctest``
.indent
Simple to use, but susceptible to changes in output
.bullet
``unittest`` (aka PyUnit)
.indent
Class-based assertion testing
.slide
=outlinelevel 2
@head ``warnings``
.bullet
PEP 230
.indent
Built on exceptions
.bullet
Filterable
.bullet
Python 2.3
.indent
Logging module planned
.slide
@head Core Language II
.bullet
Objects and Namespaces
.bullet
More Basics
.bullet
More OOP
.bullet
Advanced Features
.slide
=outlinelevel 1
@head Objects and Namespaces
.bullet
Objects
.bullet
Namespaces
.bullet
Scope
.slide
=outlinelevel 2
@head Everything an Object
.bullet
All data is object
.indent
Functions, types, classes, class instances, iterators, generators,
modules, and files all first-class objects
.bullet
Accessing objects
.indent
Targets contain bindings to objects
.indent
"Binding" used instead of "reference" because you can only deal with
objects directly, never the references themselves (except in C API).
.slide
=outlinelevel 2
@head Targets
.bullet
Names
.bullet
Attributes
.indent
Names in object space, accessed with dot notation
.bullet
Indexes / Keys
.bullet
Invisible targets
.indent
E.g. ``return``
.indent
Important for understanding refcounts
.slide
=outlinelevel 2
@head Names
.bullet
What's a name?
.indent
Bare word at builtin, module global, or function scope (more on scope
shortly)
.bullet
"Name" instead of "variable"
.indent
Names in Python don't contain data, only bindings; they're used like
variables, mostly, but use "names" instead to remember the semantics
.spacer
=height 10
.indent
Up to now I've used "variable" for convenience; from now on I'm
using "name"
.slide
=outlinelevel 2
@head Each Object a Namespace
.bullet
Attributes
.indent
Objects contain dict that maps key/value pairs to names and objects.
For some objects (primarily built-in types, function locals, and some
new-style classes), dict gets mapped to vector for speed.
.slide
=outlinelevel 2
@head Namespace Demo
.bullet
``namespace.py``
.slide
=outlinelevel 2
@head Binding Operations
.bullet
Creating names
.indent
``=``, ``for`` (and list comprehensions), ``def``,
``import`` (all forms), ``class``, function/method calls
.bullet
Other targets
.indent
``=`` (sequences/maps), ``return``, ``yield``, ``print``,
function/method default parameters
.slide
=outlinelevel 2
@head Scope
.bullet
Two scope hierarchies:
.indent
Execution scope
.indent
Class inheritance scope
.bullet
Read-only
.indent
Non-local scopes are read-only unless made explicit
.slide
=outlinelevel 2
@head Execution Scope
.bullet
Searches three namespaces in order:
.indent
Function local
.indent
Module global
.indent
Builtins
.bullet
Shadowing
.indent
Locals shadow globals shadow builtins
.slide
=outlinelevel 2
@head Nested Scopes
.bullet
Functions inside functions
.bullet
Lexical scope
.indent
Not dynamic scope
.indent
Allows static analysis of source code
.bullet
Python 2.1
.indent
``from __future__ import nested_scopes``
.slide
=outlinelevel 2
@head Class Inheritance Scope
.bullet
Non-standard "scope" usage
.indent
Some people disagree with me that class attribute/method inheritance
should be discussed as a "scope", but it's a convenient way to explain
the similarities with execution scope
.bullet
``self``
.indent
Used in methods to distinguish between execution scope (implicit as
in other functions) and the class inheritance scope (which is made
explicit through ``self``)
.slide
=outlinelevel 2
@head Scope Demo
.bullet
``scope.py``
.slide
=outlinelevel 2
@head Reference Counts
.bullet
Object deletion
.indent
Objects are deleted when no bindings reference object
.bullet
Binding
.indent
Each binding increases refcount
.bullet
Decrease refcount
.indent
Rebinding
.indent
Name going out of scope
.indent
``del``
.slide
=outlinelevel 2
@head ``del``
.bullet
Deletes bindings, not objects
.bullet
Can ``del`` parts of objects:
.spacer
=height 10
.code
>>> L = [1,2,3,4,5]
>>> del L[1:3]
>>> L
[1, 4, 5]
>>> d = {'a': 1, 'b': 2, 'c': 5}
>>> del d['b']
>>> d
{'a': 1, 'c': 5}
.slide
=outlinelevel 2
@head Refcount Cascades
.bullet
Objects refer to other objects
.indent
When an object gets deleted, all objects it refers to get decremented and
may be deleted -- leading to a cascade of object deletions
.bullet
Large lists/dicts
.indent
Deleting a large list or dict can take a long time as each object in the
list/dict needs to be walked
.slide
=outlinelevel 2
@head GC
.bullet
Garbage collection
.indent
In addition to refcount, not instead of
.bullet
Cycles
.indent
Occur when two or more objects refer to each other; refcount
can't go to zero. If no external targets refer to cycle,
cycle is "garbage" and GC deletes it.
.bullet
``__del__()``
.indent
Objects with ``__del__()`` can't be GC'd because of ordering problem
.slide
=outlinelevel 2
@head Refcount/GC Demo
.bullet
``refcount_gc.py``
.slide
=outlinelevel 1
@head More Basics
.bullet
Operators
.bullet
Handling Globals
.slide
=outlinelevel 2
@head Using ``==``
.bullet
``=`` vs. ``==``
.indent
Can't use assignment in expression
.bullet
``==`` vs. ``is``
.indent
Use ``is`` only for object identity comparisons or for builtin
singletons (``None``, ``True``, ``False``)
.slide
=outlinelevel 2
@head Short Circuit
.bullet
Evaluates until truth known
.bullet
``and``
.indent
Evaluates until first false expression
.bullet
``or``
.indent
Evaluates until first true expression
.bullet
Example:
.indent
``f() and g()``
.indent
Calls ``g()`` only if ``f()`` true
.slide
=outlinelevel 2
@head True / False
.bullet
False
.indent
``None``, ``False``, ``""``, ``0``, ``[]``, ``()``
.indent
``__nonzero__()`` or ``__len__()`` return ``0``
.bullet
True
.indent
Everything else
.bullet
Truth testing
.indent
If you just want the truth value of an object, always use the
bare object in a conditional:
.spacer
=height 15
.indent
``if obj:``
.slide
=outlinelevel 2
@head Mutability Again
.bullet
``=`` vs. ``+=`` (and family)
.indent
``=`` always [re]binds
.indent
mutable vs. immutable
.indent
``+=`` may rebind ``self`` to original object if mutable
.slide
=outlinelevel 2
@head Handling Globals
.bullet
``global``
.bullet
Class or class instance
.bullet
Module
.indent
Best for sharing globals across multiple modules
.spacer
=height 15
.bullet
Gang of Four
.indent
Modules are Singleton
.slide
=outlinelevel 2
@head Globals Demo
.bullet
``globals.py``
.slide
=outlinelevel 1
@head More OOP
.bullet
Class special methods
.bullet
New-style classes
.slide
=outlinelevel 2
@head Class Special Methods
.bullet
Double underscore
.indent
Special methods have double underscore on both sides of their name
(e.g. ``__init__()``); do not use double-underscores for your own
methods
.indent
Leading double underscore causes name-mangling so derived classes can't
access directly
.slide
=outlinelevel 2
@head Syntactic Sugar
.bullet
Syntax becomes method calls
.indent
``a[i]`` => ``a.__getitem__(i)``
.indent
``a[i] = x`` => ``a.__setitem__(i, x)``
.indent
``a + b`` => ``a.__add__(b)``
.indent
``a += b`` => ``a = a.__iadd__(b)``
.indent
``a()`` => ``a.__call__()``
.indent
``a(x, y, z)`` => ``a.__call__(x, y, z)``
.indent
``float(a)`` => ``a.__float__()``
.bullet
Accessible magic
.indent
Just set up the right methods for your classes
.slide
=outlinelevel 2
@head switch/case
.bullet
Dict-based function dispatch
.spacer
=height 10
.code
def red(): pass
def green(): pass
def blue(): pass
dispatch = {
'red': red,
'green': green,
'blue': blue
}
dispatch[getColor()]()
.slide
=outlinelevel 1
@head New-style Classes
.bullet
PEPs 252, 253
.bullet
Classic can't subclass builtin types
.indent
New-style classes also referred to as "type/class unification"
.bullet
New-style classes have other benefits:
.indent
Properties
.indent
Static/class methods
.indent
Better multiple-inheritance lookup
.indent
Memory-saving ``__slots__``
.indent
Easier metaclasses
.slide
=outlinelevel 2
@head Properties
.bullet
Classic classes
.indent
Need to use ``__getattr__()`` and ``__setattr__()``, both of which are
clumsy (and usually overkill)
.bullet
New-style classes
.indent
Property methods can be created for individual attributes
.slide
=outlinelevel 2
@head Static/class methods
.bullet
Static method
.indent
Plain function in class namespace
.bullet
Class method
.indent
Like instance method, except takes class argument
.slide
=outlinelevel 2
@head Multiple-Inheritance Lookup
.bullet
Shared ancestor
.spacer
=height 10
.code
class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass
.spacer
=height 10
.indent
Classic classes visit A twice: DBACA
.indent
New-style classes visit A once: DBCA
.slide
=outlinelevel 2
@head ``__slots__``
.bullet
Attributes
.indent
Normally stored in dict
.indent
``__slots__`` creates indexed vector
.indent
Attribute names stored with class, not instance
.slide
=outlinelevel 2
@head Metaclasses
.bullet
Not touching
.indent
I'm a Python expert, not a wizard
.indent
Definition: metaclasses allow changing the way classes get instantiated
(yes, classes, not class instances)
.indent
Use case: creating classes dynamically for a simulation (but Python's
dynamic introspection features usually don't require metaclasses for
this)
.slide
=outlinelevel 2
@head New-style Class Demo
.bullet
``newstyle.py``
.slide
=outlinelevel 1
@head Advanced Features
.bullet
Iterators
.bullet
Generators
.bullet
List Comprehensions
.slide
=outlinelevel 2
@head Iterators
.bullet
PEP 234
.bullet
Efficiency
.indent
Iterators don't need to create entire sequence
.bullet
Protocol
.indent
``next()`` method
.indent
``raise StopIteration`` when finished
.slide
=outlinelevel 2
@head Using Iterators
.bullet
``iter()``
.indent
Create iterator by calling ``iter()`` on object or instantiating a
class documented to be iterator
.indent
``for`` implicitly calls ``iter()``
.indent
``iter()`` on iterator usually returns same iterator
.bullet
``iter(func, sentinel)``
.indent
Use ``iter()`` on regular functions
.indent
``func`` is a function object, not function call
.indent
Discouraged -- use generators if possible
.slide
=outlinelevel 2
@head Iterator Demo
.bullet
``iterator.py``
.slide
=outlinelevel 2
@head Generators
.bullet
PEP 255
.bullet
Function creates iterator
.indent
Generator is a resumable function that maintains its local state between
callbacks into the function (through the iterator interface)
.bullet
``yield``
.indent
Keyword turns function into generator
.indent
``from __future__ import generators`` (not needed in Python 2.3+)
.slide
=outlinelevel 2
@head Generator Demo
.bullet
``generator.py``
.bullet
Notice
.indent
Generator code doesn't execute until first call to iterator's ``next()``
.indent
Each call to generator creates new iterator
.slide
=outlinelevel 2
@head Generator Pipelines
.bullet
Pipelines save memory
.bullet
``genpipe.py``
.indent
Imports ``grep`` and ``uniq``
.slide
=outlinelevel 2
@head Iterators vs. Generators
.bullet
Generators simpler
.indent
No need to explicitly maintain state
.bullet
Iterators more controllable
.indent
Can manipulate internal state (e.g., ``__del__`` method on iterator object
to handle cleanup)
.indent
Can be backported to older versions of Python
.slide
=outlinelevel 2
@head List Comprehensions
.bullet
Syntax
.spacer
=height 10
.code
[ for in
for in ...
for in
if ]
.bullet
Equivalent to
.spacer
=height 10
.code
L = []
for in :
for in :
...
for in :
if :
L.append()
return L
.slide
=outlinelevel 2
@head List Comp Examples
.bullet
Beware obfuscation
.indent
Remember: "Readability counts"
.spacer
=height 15
.code
>>> names = ["joe", "martha", "frank", "elizabeth"]
>>> [n.capitalize() for n in names]
['Joe', 'Martha', 'Frank', 'Elizabeth']
>>> [n for n in names if len(n)<6]
['joe', 'frank']
>>> # cross-product
... [(i,n) for i in range(2) for n in names]
[(0, 'joe'), (0, 'martha'), (0, 'frank'),
(0, 'elizabeth'), (1, 'joe'), (1, 'martha'),
(1, 'frank'), (1, 'elizabeth')]
.slide
@head Library
.bullet
Core
.bullet
Text Processing
.bullet
Essential 3rd Party
.indent
GUIs
.indent
Other
.slide
=outlinelevel 1
@head Core modules
.bullet
``__builtin__``
.bullet
``sys``
.bullet
``os``
.slide
=outlinelevel 2
@head ``__builtin__``
.bullet
``__builtin__`` vs ``__builtins__``
.indent
``__builtin__`` is the actual module; ``__builtins__`` is the alias used
for creating the namespace lookup
.spacer
=height 10
.indent
Main reason is to avoid getting flooded with output when calling
``vars()`` in interactive mode.
.slide
=outlinelevel 2
@head ``os``
.bullet
Somewhat Unix-centric
.indent
E.g. ``rm`` -> ``os.remove()``
.bullet
Other platforms emulated
.indent
Read the docs
.slide
=outlinelevel 1
@head Text processing
.bullet
String manipulation
.bullet
Unicode
.slide
=outlinelevel 2
@head String Manipulation
.bullet
Interpolation
.bullet
String methods
.bullet
Regexes
.bullet
Text parsers
.slide
=outlinelevel 2
@head Interpolation
.bullet
Similar to C printf()
.spacer
=height 10
.code
>>> '%s, %s!' % ('Hello', 'world')
'Hello, world!'
>>> '%10s' % "Hi!"
' Hi!'
>>> '%.4f' % 1.5
'1.5000'
>>> d = {'foo': 1, 'bar': 5}
>>> 'Bar is %(bar)s' % d
'Bar is 5'
.slide
=outlinelevel 2
@head String Methods
.bullet
``s.method()`` vs. ``string.func(s)``
.indent
String methods are favored over the ``string`` module unless backward
compatibility is required
.bullet
Important methods
.indent
``join()``, ``split()``, ``find()``, ``replace()``,
``startswith()``, ``endswith()``
.indent
``translate()`` (but needs ``string.maketrans()``) -- fastest way to
delete characters
.slide
=outlinelevel 2
@head Building Strings
.bullet
Avoid '``+``'
.indent
Especially in loops; Python immutable strings get constantly copied
.bullet
Use
.indent
Interpolation
.indent
``join()``
.slide
=outlinelevel 2
@head Regex
.bullet
``re`` module
.indent
P/P: regex syntax almost identical
.spacer
=height 5
.indent
Use objects/methods to access functionality
.spacer
=height 5
.indent
P/P: Python uses string methods instead of regexes for many common
operations
.bullet
Python extensions
.indent
``(?P``
.indent
Named groups
.slide
=outlinelevel 2
@head ``match()`` vs. ``search()``
.bullet
``match()``
.indent
Matches only at start of string (or at optional position parameter); not
quite equivalent to using "^"
.bullet
``search()``
.indent
Standard regex search
.slide
=outlinelevel 2
@head Regex Demo
.bullet
``regex.py``
.slide
=outlinelevel 2
@head Text Parsers
.bullet
Regex vs. nested input
.indent
Regexes don't handle nested constructs (e.g. XML) particularly well;
text parsers are designed to handle this task
.bullet
Parsers
.indent
mxTextTools
.indent
SPARK
.slide
=outlinelevel 2
@head Unicode
.bullet
Encode/decode
.indent
90% of the Unicode problems I've seen come from getting the encode and
decode steps messed up
.spacer
=height 15
.code
>>> unicode('Andr\x82')
Traceback (most recent call last):
File "", line 1, in ?
UnicodeError: ASCII decoding error:
ordinal not in range(128)
>>> unicode('Andr\x82', 'latin-1')
u'Andr\x82'
.slide
=outlinelevel 1
@head 3rd Party Modules
.bullet
GUI
.bullet
Others
.slide
=outlinelevel 2
@head GUI Libraries
.bullet
Tkinter (default)
.bullet
wxPython
.bullet
PyGame (SDL)
.bullet
PyOpenGL
.bullet
PyQt
.slide
=outlinelevel 2
@head Other 3rd Party
.bullet
mxODBC
.bullet
PIL
.indent
Python Imaging Library
.bullet
win32all
.bullet
NumPy
.bullet
PyChecker
.slide
@head Resources
.bullet
Main URL
.url
http://www.python.org/
.bullet
Tutorial
.url
http://www.python.org/doc/current/tut/tut.html
.bullet
PEPs
.url
http://www.python.org/peps/
.bullet
Perl/Python conversion
.url
http://starship.python.net/crew/da/jak/cookbook.html
.slide
=outlinelevel 1
@head Help
.bullet
``comp.lang.python`` / ``python-list``
.indent
Bread-and-butter support
.bullet
``tutor@python.org``
.indent
Good way to solidify your own knowledge by helping beginners
.slide
=outlinelevel 1
@head Reference Books
.bullet
Programming Python
.bullet
Core Python Programming
.bullet
Python in a Nutshell
.indent
Not yet out
.bullet
Python Essential Reference
.indent
If you get just one book, this is it
.slide
=outlinelevel 1
@head Other Books
.bullet
Python Cookbook
.indent
Similar to Perl Cookbook
.bullet
Python Programming on Win32
.slide
=outlinelevel 1
@head Text Processing
.bullet
SPARK
.url
http://pages.cpsc.ucalgary.ca/~aycock/spark/
.bullet
mxTextTools
.url
http://www.lemburg.com/files/python/mxTextTools.html
.slide
=outlinelevel 1
@head Advanced Topics
.bullet
New-style classes
.url
http://www.python.org/2.2.1/descrintro.html
.bullet
Unicode
.url
www.reportlab.com/i18n/python_unicode_tutorial.html
.bullet
Regexes
.indent
``re`` module
.slide
=outlinelevel 1
@head GUIs
.bullet
Tkinter
.url
http://www.python.org/topics/tkinter/
.bullet
wxPython
.url
http://www.wxpython.org/
.bullet
PyGame (SDL)
.url
http://www.pygame.org/
.bullet
PyOpenGL
.url
http://pyopengl.sourceforge.net/
.bullet
PyQt
.url
http://www.riverbankcomputing.co.uk/pyqt/index.php
.slide
=outlinelevel 1
@head 3rd Party Libs
.bullet
mxODBC
.url
http://www.lemburg.com/files/python/mxODBC.html
.bullet
PIL
.url
http://www.pythonware.com/products/pil/
.bullet
win32all
.url
http://starship.python.net/crew/mhammond/
.bullet
NumPy
.url
http://numpy.sourceforge.net/
.bullet
PyChecker
.url
http://pychecker.sourceforge.net/
.slide
=outlinelevel 1
@head Dev resources
.bullet
Dev guide
.url
http://www.python.org/dev/
.bullet
Version differences
.url
http://www.amk.ca/python/
.indent
PEP 290
.slide
=outlinelevel 1
@head Jython
.bullet
Main
.url
http://www.jython.org/
.bullet
Jython vs. CPython
.url
http://jython.sourceforge.net/docs/differences.html