overview

  • simple, readable high-level language
  • interpreted; cached compiled bytecode
  • garbage collected
  • whitespace significant for block structure
  • implementations
    • CPython, jython, IronPython, PyPy
    • Nokia 770, Nokia "Series 60" phones
    • Unix/Linux, Windows, Mac, PalmOS, WindowsCE, RiscOS, VxWorks, QNX, OS/2, OS/390, AS/400, PlayStation, Sharp Zaurus, BeOS, VMS
  • Guido van Rossum - Benevolent Dictator For Life (BDFL)
  • Python Enhancement Proposals (PEPs)

type

  • hybrid: procedural, object-oriented, with functional concepts
  • dynamically typed: variables are whatever they contain; no definitons of their type
  • strongly typed: numbers are numbers, strings are strings. 1 + "1" fails, no automatic coersion
  • duck-typed: no explicit (object) interface checks or failures; "if it (behaves) like a duck..."

datatypes

  • None, integers, long, boolean, float, string
  • no sigils on identifiers; `global` keyword
  • tuple - (x,y,z), (x,) - foo[0]
  • list - [1,2,3] - foo[0], foo[0:1], foo[0:1:2]
  • dictionary - {'a': 1, 'b': 2} - foo['a']
  • set
  • callables/functions
  • classes

quoting

  • single: 'jsled', "jsled"
  • triple: '''josh 'bob' sled'''
    • newlines automagically quoted, nice for multi-string text.
  • raw: r'\n''\\n'
  • unicode

multiple assignment

>>> a,b,c = 1,2,3
>>> foo = 1,2,3
>>> a,b,c = foo
>>> foo = {'a': 1, 'b': 2, 'c': 3}
>>> for key,val in foo.iteritems():
    print key,val
a 1
b 2
c 3

control structures

  • pass
    • no-op filler ... ';' in other languages.
  • if condition:/elif condition:/else:
  • for var in sequence:, else:
        for ($i = 0; $i < 10; $i++)
    ...
    for i in range(10): ...
  • while condition:, else
  • break, continue

function definition

def foo(spam, eggs):
    rtn = spam + eggs
    print 'about to return',rtn
    return rtn

foo(1,2)

bar = foo
bar(3,4)

lambda x,y: x + y

advanced functions

  • default arguments
    def foo(spam, eggs=42):
      ....
    foo('jsled')
    
  • variable, keyword arguments
    def foo(foo, *more, **by_name):
        ...
    
    >>> foo(1, 3, bar=2)
    foo: 1
    more: (3,)
    by_name: {'bar':2}
    

class definition

  • constructor: __init__(self, args)
  • super-class calls (no syntactic sugar): <super.method>(self, args)
  • 'this' object is first argument to methods; "self" by convention only.

Class example

class Foo (Bar):
    def __init__(self, name):
        self.name = name
        Bar.__init__(self)
    def get_name(self):
        return self.name
    def bars_thing(self, arg):
        return Bar.bars_thing(self, arg)

x = Foo('jsled')
x.get_name()

list comprehensions

  • language support for common list processing idioms
  • transformation
    • [f(x) for x in [1,2,3]]
  • filtering
    • [x for x in [1,2,3] if x % 2 == 0]
  • bar = \
      dict([(k,f(v)) for k,v in foo.iteritems() \
            if k in ['these', 'are', 'allowed']])

iterators

  • for x in obj: ...
    • iter = obj.__iter__()
  • iter.next()
    • raise StopIteration

generators

def gen_odd_numbers(limit):
    i = 1
    while i < limit:
        yield i
        i += 2

>>> [x for x in gen_odd_numbers(10)]
[1, 3, 5, 7, 9]
>>> [x for x in (x**2 for x in range(10))]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

standard methods

  • str(foo)foo.__str__()
  • len(foo)foo.__len__()
  • indexing
    • foo['a'] foo.__getitem__('a')
    • foo['a'] = 'b'foo.__setitem__('a', 'b')
    • del foo['a'] foo.__delitem__('a')
  • Most operators translate into __operation__s.

idiom overloading

class WorldWideWeb:
    def __getitem__(url):
        page = ''
        http_connection = urllib2.open(url)
        while (fragment = http_connection.read(1024)):
           page = page + fragment
        return page

web = WorldWideWeb()
page = web['http://uvm.org/vague/']

doc strings

  • module eg.py:
    __doc__ = 'Example Code.'
    
    def foo():
         "foo does awesome things"
         return do_awesome_thing()
    
    
>>> import eg
>>> eg.foo.__doc__ 
>>> 'foo does awesome things'
>>> help(eg)
Help on module eg:

NAME
    eg - Example code.

FILE
    /home/user/path/to/eg.py

FUNCTIONS
    foo()
        foo does awesome things

decorators

def memoize(fn):
    cache = {}
    def _memoized(*args, **kwargs):
        memo = repr((args,kwargs))
        if cache.has_key(memo):
            return cache[memo]
        rtn = fn(*args, **kwargs)
        print 'saving', rtn
        cache[memo] = rtn
        return rtn
    return _memoized

decorators

@memoize
def foo(arg):
    return arg

>>> foo(42)
saving 42
42
>>> foo(42)
42
  • python 2.4

dir()

  • lists names that a thing defines
    • thing = module, object, callable
  • >>> dir('')
    ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

exceptions, try/except/finally

try:
    block-1 ...
except Exception1:
    handler-1 ...
except Exception2:
    handler-2 ...
else:
    else-block
finally:
    final-block
  • finally in python 2.5

`with` statement

  • db_connection = DbConnection()
    with db_connection as cxn:
        cxn.execute_statement(sql_query)
    
  • protocol:
    cxn = db_connection.__enter__()
    try:
        cxn.execute_statement(sql_query)
    except Exception, e:
        res = db_connection.__exit__(type, value, traceback)
        if not res: raise e
    
  • python 2.5

modules, packages

  • A 'module' is a file containing python code.
  • The module name is the name of the file, without the '.py'.
  • Packaging is a way of name-spacing modules.
    • Package name parts are directories with '__init__.py' files.
  • e.g.,
    ./mumble.py  # def do_stuff(): ...
    ./Foo/
          __init__.py
          core.py # def do_stuff(): ...
          opt.py  # def do_stuff(): ...
    
  • Two forms of package/module inclusion...

module importing 1

  • import <name>
    • "<name>.*" is brought the local namespace
    • >>> import mumble
      >>> mumble.do_stuff()
      
      >>> import Foo.core
      >>> Foo.core.do_stuff()
      

module importing 2

  • from <package> import <name>
  • from <package> import *
    • "<name>" is brought into the local namespace:
    • >>> from Foo import core
      >>> core.do_stuff()
      
      >>> form Foo.core import *
      >>> do_stuff()
      >>> do_more_stuff()
      
      >>> from Foo.core import do_stuff
      >>> do_stuff()
      >>> do_more_stuff()
      Error.
      

format strings

  • print '%s: %d' % ('foo', 42)
  • print '%(name)s: %(val)d' % {'name': foo, 'val': 42}
  • This is the string's __mod__ method in action.

reStructuredText

  • lightweight plain-text "markup" language
  • Document Title
    --------------
    
    Introduction
    ============
    
    The following document will cover:
    - the hopes
      - the dreams
    - the attempt
    - the failings
    
    With special thanks to `our contributors`_.
    
    .. _`our contributors`: acknowledgements.html
    

packaging

  • distutils module
  • top-level setup.py file, import distutils
  • modules, scripts, package-data, meta-data
  • supports compilation of C/C++/ObjC extensions
  • py2exe, py2app
    • distutils extension
    • create a self-contained Win32 .exe / Mac OS X app
    • python, dependent libs and app itself
  • Eggs
    • eggs : Python :: jars : Java

distutils example

from distutils.core import setup
setup(name='Distutils',
      version='1.0',
      description='Python Distribution Utilities',
      author='Greg Ward',
      author_email='gward@python.net',
      url='http://www.python.org/sigs/distutils-sig/',
      packages=['distutils', 'distutils.command'],
     )

stdlib, "batteries included"

  • file-like objects, OS interface (spawn/exec, mk/rmdir, get/set-e)uid, getcwd, ...), args, paths
  • regular expressions
  • date/time
  • zlib/gzip/bz2/zipfile/tarfile, base64/uuencoding,
  • smtplib, urllib (urllib2), email, mime-handling, smtp, ftp, rfc822, xmlrpc, cgi
  • doctest (docstrings -> unit tests) , unittest
  • threading, queues, locks (relatively primative)
  • logging (log4j-style)
  • xml parsing/generation
  • audio handling
  • python code parsing, evaluation

stdlib

sys, gc, weakref, fpectl, atexit, types, UserDict, UserList, UserString, operator, inspect, traceback, linecache, pickle, cPickle, copy_reg, shelve, copy, marshal, warnings, imp, zipimport, pkgutil, modulefinder, code, codeop, pprint, repr, new, site, user, __builtin__, __main__, __future__, string, re, struct, difflib, fpformat, StringIO, cStringIO, textwrap, codecs, unicodedata, stringprep, pydoc, doctest, unittest, test, test.test_support, decimal, math, cmath, random, whrandom, bisect, collections, heapq, array, sets, itertools, ConfigParser, fileinput, calendar, cmd, shlex, os, os.path, dircache, stat, statcache, statvfs, filecmp, subprocess, popen2, datetime, time, sched, mutex, getpass, curses, getopt, optparse, tempfile, errno, glob, fnmatch, shutil, locale, gettext, logging, platform, signal, socket, select, thread, threading, dummy_thread, dummy_threading, Queue, mmap, anydbm, dbhash, whichdb, bsddb, dumbdbm, zlib, gzip, bz2, zipfile, tarfile, readline, rlcompleter, posix, pwd, grp, crypt, dl, dbm, gdbm, termios, tty, pty, fcntl, pipes, posixfile, resource, nis, syslog, commands, hotshot, timeit, webbrowser, cgi, cgitb, urllib, urllib2, httplib, ftplib, gopherlib, poplib, imaplib, nntplib, smtplib, smtpd, telnetlib, urlparse, SocketServer, BaseHTTPServer, SimpleHTTPServer, CGIHTTPServer, cookielib, Cookie, xmlrpclib, SimpleXMLRPCServer, DocXMLRPCServer, asyncore, asynchat, formatter, email, mailcap, mailbox, mhlib, mimetools, mimetypes, MimeWriter, mimify, multifile, rfc822, base64, binascii, binhex, quopri, uu, xdrlib, netrc, robotparser, csv, HTMLParser, sgmllib, htmllib, htmlentitydefs, xml.parsers.expat, xml.dom, xml.dom.minidom, xml.dom.pulldom, xml.sax, xml.sax.handler, xml.sax.saxutils, xml.sax.xmlreader, xmllib, audioop, imageop, aifc, sunau, wave, chunk, colorsys, rgbimg, imghdr, sndhdr, ossaudiodev, hmac, md5, sha, Tkinter, Tix, ScrolledText, turtle, rexec, Bastion, parser, symbol, token, keyword, tokenize, tabnanny, pyclbr, py_compile, compileall, dis, pickletools, distutils, sqlite3, wsgiref

bindings, major libs

  • DB API
    • DB/2, Informix, Ingres, JDBC (jython), MySQL, Postgres (x3), ODBC (3x), Oracle (x3), sqllite (stdlib), SAP, Sybase
  • Windowing
    • tkinter, MFC, wxPython, pyGtk, PyQt
  • Desktop
    • gnome-python-bindings, PyKDE, COM
  • Networking
    • stdlib, Twisted

more bindings...

  • Graphics/Games
    • PyOpenGL, PyGame (SDL), PyKyra, PIL
  • Misc
    • python.boost - Boost peer-reviewed C++ library.
  • Apps
    • anaconda (partly), portage (partly), blender (xplatform 3d modeling)

Simple PyGTK application

import gtk
   
win = gtk.Window()
win.add(box)
box = gtk.VBox()
button = gtk.Button('Click me!')
def clicked_cb(button):
    print 'I was clicked!!!'
button.connect('clicked', clicked_cb)
box.add(button)
win.connect('destroy', lambda win: gtk.main_quit())
win.show_all()
gtk.main()

Web Frameworks

  • WSGI : "CGI" for Python
    • mod_python
  • application logic
    • CherryPy, web.py, Myghty (HTML::Mason), Quixote, Webware
  • templating
    • Cheetah, Kid, Myghty, Airspeed (Velocity compatible)
  • "Ruby-on-Rails"-like
    • TurboGears: CherryPy (MVC/app), Kid (templating), SQLObject (ORM), MochiKit (AJAX)
    • Django: (homebrew all around)
  • Zope, Plone

The Python Programming Language





fin