Vitanuova for 2003 October 30 (entry 0)

< Madness
Leonard >

I went to Stanford with some of my EFF colleagues to hear Guido van Rossum speak. He was apparently your architypical "I am just a nice guy who happened to create a major part of the world's IT infrastructure" sort. (Andrew Tridgell also comes to mind in this capacity.)

I had actually heard Guido speak at a Linux event sometime around 1999, before I became a Python programmer. At that time, people kept asking him why there was a string module rather than having strings be objects with methods to perform common string operations.

Of course, that eventually got changed:

[schoen@zork(~)] python1.5
Python 1.5.2 (#0, Jul  5 2003, 11:45:08)  [GCC 3.3.1 20030626 (Debian prerelease)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> dir("foo")
[]
>>>
[schoen@zork(~)] python2.3
Python 2.3.2 (#2, Oct  6 2003, 08:02:06)
[GCC 3.3.2 20030908 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dir("foo")
['__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', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

What's more, the string module (in which you find all these functions in Python 1.5) has been deprecated and is slated to be removed entirely in Python 3.0. So I guess the people at that old talk got their wish!

Anyway, when I heard Guido speak in 1999, I had little or no idea what he was talking about. All I knew was that Python was a language many of my friends were getting interested in, and this guy was responsible for it. This time around, I was an actual Python programmer. I found the substance of Guido's talk quite clear and helpful. Among other things, he spent a good deal of time explaining the motivation for and implementation of generators.

A generator is a Python function that uses the "yield" statment. (Well, technically it's the object returned by calling such a function.) It allows you, in a succint way, to write a function with a complicated internal state that returns values sequentially in such a way that they can be used by another complicated function. Without generators, you would probably need to have a complicated data structure to save state in order to switch back and forth constantly between the complicated function and the thing it's callig. With generators, Python saves that state for you. Of course, Lisps had something like this first.

I haven't thought of a non-trivial application for generators, but maybe I would if I were writing some non-trivial programs. (All the things written about generators use parsers as a sample application, but as it happens I never seem to write any parsers...)

It's fun to have the language implementer himself explain language features to you!


Links from other weblogs:

Sat Feb 24 11:35:30: XQhaYJuxJmVf from OjZiDBhFg

dqVmaWLmLuv MYJVkT [URL=http://xindnsy.com/]sjEhjmkM[/URL]


[Main]
Support Bloggers' Rights!
Support Bloggers' Rights!


Contact: Seth David Schoen