Introducing Python to others

Delaney, Timothy (Tim) tdelaney at avaya.com
Thu Mar 26 20:06:51 EDT 2009


Rhodri James wrote:

> On Thu, 26 Mar 2009 09:35:55 -0000, Paddy O'Loughlin
> <patrick.oloughlin at gmail.com> wrote:
> 
>> Because of this, I was thinking of making sure I included exceptions
>> and handling, the richness of the python library and a pointing out
>> how many modules there were out there to do almost anything one could
>> think of.
> 
> Once you've shown them exceptions, show them context managers briefly.
> It's amazing how much they can simplify some types of exception
> handling. 

I would second this. Basically, write something in idiomatic PHP or
Java, and write the direct translation to Python. Then show them the
idiomatic Python.

Of course, you need to be upfront that this may in some cases work in
reverse, but that in your experience idiomatic Python code that has the
same effect tends to be considerably clearer and shorter.

try/finally -> with is an excellent example of idiomatic python. I'd
suggest IO would be the best candidate - something along the lines of:

    f = open('path', 'w')

    try:
        f.write('Hello, world!')
    finally:
        f.close()

to:

    with open('path', 'w') as f:
        f.write('Hello, world!')

Mention that locks (threading) are also context managers, there's a
built-in "closing" context manager (and what it does), and explain how
you can write any context manager you like.

Tim Delaney



More information about the Python-list mailing list