Introducing Python to others

Beni Cherniavsky beni.cherniavsky at gmail.com
Sat Mar 28 22:51:38 CET 2009


Small additions:

On Mar 26, 7:35 pm, "J. Cliff Dyer" <j... at sdf.lonestar.org> wrote:
>
> 2) Aliasing imports is also cool.  Show people how easy it is to switch
> from
>
> >>> import MySQLdb as db
> to
> >>> import psycopg2 as db
>
> and have all your dbapi2 code still work.  Or from
>
> >>> from StringIO import StringIO
> to
> >>> from cStringIO import StringIO
>
For even cooler effect, show them that it can be decided at run time:

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

> 3) Functions as first-class variables (which could connect to a
> discussion of decorators or of dictionaries as dispatch tables).
>
If you show this, show bound methods as first-class values.

Explain how class and module namespaces are really simple dicts.

> 4) List comprehensions
>
> 5) Generators using yield
>
And generator expressions.  Show off things like sum(x**2 for x in
range(10)).

When showing generators, I like to explain how Python allows you to
build abstractions - take common patterns and give them names:

- generator + for loop = abstraction over while loop
- context manager + with statement = abstraction over try statement
- decorators = abstraction over def statement
- metaclasses = abstraction over class statement
  (you don't have time to show metaclasses, but you can mention their
existance)

---

Unrelated cool stuff to show off: doctest, VPython



More information about the Python-list mailing list