Example Tutorial Proposal
The following is an actual tutorial proposal from 2007, reworked to conform to the 2008 proposal requirements.
Contents
Code Like a Pythonista: Idiomatic Python
Presenter: David Goodger (goodger@python.org, +1-555-123-4567)
Intended audience: beginning to intermediate Python programmers. Familiarity with Python's syntax is assumed.
Tutorial format: Interactive lecture, with lots of short examples that attendees can try out on the fly. I will take questions throughout the tutorial.
Recording: I give permission to record and publish my tutorial for free distribution.
Requirements: Attendees are welcome to bring their laptops with Python installed (version 2.4 or higher).
Notes for Reviewers
In the tutorial I presented at PyCon 2006 (Text & Data Processing), I was surprised at the reaction to two techniques I used:
- Advanced string formatting with "%" ("%(name)s", "locals()")
- Decorate-sort-undecorate
Many of the attendees were unaware of these tools that many experienced Python programmers use without thinking. This tutorial will present these and many other idioms & techniques that beginning programmers can benefit from immediately.
Promotional Summary
Are you comfortable with Python's syntax, but have yet to master its idioms? Does Python still feel a bit like a foreign language? Are you looking for more "elegance" for your programs?
In this interactive tutorial, we'll cover many essential Python idioms and techniques in depth, adding immediately useful tools to your belt. Rationale will be provided for all idioms -- the "why" in addition to the "what & how". We'll run through lots of small, practical, hands-on examples. I will take questions throughout the tutorial.
Attendees should bring a laptop if possible, to try out the techniques they learn.
Outline for Review
Introduction: Zen of Python (10 minutes)
Good "rules of thumb", open to interpretation.
When in doubt, import this.
Coding style (10 minutes)
- PEP 8
- Naming:
- joined_lower for functions, methods, attributes
- joined_lower or ALL_CAPS for constants
- StudlyCaps for classes
- camelCase only to conform to pre-existing conventions
- Attributes: interface, _internal, __private
- Don't use __private -- you'll regret it later
- Line continuations:
- use parentheses/brackets/braces
- backslashes are fragile; use as last resort
- spaces; no hard tabs
- docstrings vs. comments
Idiom potpourri (30 minutes)
swap values (a, b = b, a)
Build strings as a list and use ''.join at the end.
Use key in dict, not dict.has_key():
# do this: if key not in d: d[key] = [] # not this: if not d.has_key(key): d[key] = []dict.setdefault
DefaultDict (2.5)
truth vs. ==:
# do this: if x: pass # not this: if x == True: passfor line in file (modulo mixing .next & .read* methods, exception in 2.5)
enumerate
from module import * -- NOT
Advanced string formatting with "%" (10 minutes)
- %(name)s
- locals()
Decorate-sort-undecorate (15 minutes)
List comprehensions & generator expressions (10 minutes)
- clear & concise, up to a point
- when to use loops instead
Generators: simplify sequence/iterator handling (20 minutes)
- how generators work, under the hood
- how a for loop really works (including the else clause)
- example with CSV pre-filter & post-filter generators
EAFP vs. LBYL (10 minutes)
- exceptions
- duck typing
Scripts & modules (30 minutes)
Importable module & executable script:
if __name__ == '__main__':
When imported, a module's __name__ attribute is the module's file name (without ".py"). When executed as a script, __name__ is set to "__main__".
Except for special cases, don't put any regular executable code at the top-level. Put code in functions, classes, methods, and guarded by if __name__ == '__main__'.
Module structure:
"""module docstring""" # imports # constants # exception classes # interface functions # classes # internal functions & classes if __name__ == '__main__': status = main() sys.exit(status)Command-line processing: cmdline.py
Packages (15 minutes)
- Used to organize your project.
- Reduces entries in load-path.
- Reduces import name conflicts.
- Absolute and relative imports in Python 2.5
Q&A and time overrun buffer (20 minutes)
Extra topic if there's time: Classes vs. functions, modules vs. packages
Outline for Website
- Introduction: Zen of Python
- Coding style
- Idiom potpourri: Strings, dictionaries, booleans, sequences, etc.
- Advanced string formatting with "%"
- List comprehensions & generator expressions
- Sorting, decorate-sort-undecorate
- Generators: simplify sequence/iterator handling
- EAFP vs. LBYL
- Scripts & modules
- Packages
- Q&A
Presenter Bio
David Goodger is a full-time Python programmer working in the financial sector, founder and architect of the Docutils project and reStructuredText, an editor of the Python Enhancement Proposals (or PEPs), an organizer of PyCon 2007, a member of the Python Software Foundation, a Director of the Foundation for the past year, and its Secretary. David lives near Montreal with his wife and two children.
Presenter's Previous Experience
- Presented a tutorial at PyCon 2006: Text & Data Processing
- As a consultant I have provided training sessions, ranging from several hours to several days.
- I worked as an English teacher in Japan for two and a half years.
- Regular one-on-one mentoring and occasional in-house group training sessions are part of my job.
.