ONLamp.com
oreilly.comSafari Books Online.Conferences.

advertisement


Getting Loopy with Python and Perl

by Aahz
06/27/2002

This article is based in part on my O'Reilly Open Source Convention 2002 tutorial, "Python for [Perl] Programmers/" However, this article includes more Perl and Python comparison than I've included in the tutorial. My tutorial targets experienced programmers of all sorts, with the non-Python examples drawn from Perl. In this article I'll be comparing Python's loop constructs to Perl's.

Perl has two basic looping constructs: while and for/foreach. This doesn't count variations, such as "<statement> until EXPR" or do...while, nor the fact that for/foreach has two different forms. Python also has only two looping constructs: while and for. Unlike Perl, Python's loops have no variations; instead, the for loop uses a special protocol that generalizes well. Both Perl and Python have functional constructs that loop over a sequence, but that's outside the scope of this article.

The variation in available loop constructs exemplifies the basic difference between Perl and Python: Perl's motto is TMTOWTDI (There's More Than One Way To Do It), whereas Python's counter-motto is "There's Only One Way." Python's motto is the short form of one element of Python's design philosophy: "There should be one--and preferably only one--obvious way to do it." To see the rest of Python's design philosophy, start the Python interactive interpreter (Python 2.1.2 or later) and type "import this".


O'Reilly Open Source Convention -- July 22-26, San Diego, CA.

From the Frontiers of Research to the Heart of the Enterprise

Aahz will present Python for [Perl] Programmers Monday July 22nd at the 2002 O'Reilly Open Source Convention. Don't miss out, you still have time to Register!

So how do Python's looping constructs actually work? Let's start with a basic Perl idiom:

   
    while (<STDIN>) {
        print;
    }

and compare it to the equivalent Python idiom:

    
    import sys
    for line in sys.stdin:
        sys.stdout.write(line)

The main thing to notice is that Python uses a for loop instead of a while loop. There are two reasons for this:

  • Python does not permit assignment in expressions. In order to do assignment as part of the loop construct, you must use a for loop.

  • Python 2.2 introduced iterators, and a file object in Python is its own iterator. An iterator is an object that has a next() method; next() produces the elements of a sequence, one at a time. The for loop is designed to work with iterators.

Prior to Python 2.2, the loop would have been written like this:


    import sys
    while 1:
        line = sys.stdin.readline()
        if not line:
            break
        sys.stdout.write(line)

In general, Python's for loop works much like Perl's for/foreach list form. In order to loop over a sequence of numbers, you need to produce a list:


    for i in range(10):
        print i

This will print the numbers from 0 through 9. Like Perl arrays, Python lists are zero-based, and the range() function caters to that. To prove that range() is in fact creating a list of numbers, fire up the Python interpreter:

    
    >>> range(10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Contrast this with Perl's standard indexing loops:

    
    for ($i=0; $i<10; $i++) {
        print "$i\n";
    }

or

    
    foreach $i (0..9) {
        print "$i\n";
    }

Note particularly how range() specifies a value one higher than the maximum index; this makes it easy to use with the len() function. Generally speaking, range() is fast enough and consumes little enough memory that generating an entire list doesn't hurt. But if you're worried about that, Python does have the xrange() function that produces one number at a time.

Pages: 1, 2

Next Pagearrow





Sponsored by: