Greg Hewgill (ghewgill) wrote,
Greg Hewgill
ghewgill

python enhancement proposal

The other day while writing some code in Python, I had an idea. The bit of code I was writing was shaped something like this:

    for a in results:
        if a.something:
            do_action(a)

It occurred to me that if I were to write this as a list comprehension, then the "if" would combine with the "for" on the same line:

    [do_action(a) for a in results if a.something]

What if I could write this instead:

    for a in results if a.something:
        do_action(a)

This looks like a nice compact syntax for a reasonably common construct, which happens to also be nicely aligned with the list comprehension syntax. I thought I'd have a go at modifying the Python language to accept this, and perhaps even submit a PEP (Python Enhancement Proposal) for the core language.

I read about the PEP process, found a related PEP to enhance the generator syntax (from 2009), which led me to a long thread discussing that PEP, from which I found a discussion about an "if-syntax for regular for-loops" (from 2008), and finally Guido's opinion on the matter (from 2006), which states that he doesn't like it and that it was proposed and rejected before. And as it turns out, after all that I agree with Guido.

Lessons learned (none the hard way!):

  • Ideas you think are new probably aren't.
  • Do research before committing too much time or energy to a new idea.
  • Open development processes are great because there is a permanent record of what has been discussed before.
Subscribe

  • 2013 in review

    2013 is the year when everything changed. The biggest event was the birth of our daughter Lily. She was born prematurely in Shanghai while we…

  • 2012 in review

    2012 has been fairly quiet. Maybe it just seems that way because I haven't actually written anything new in this blog since last year's annual…

  • new photo galleries

    I've been busy processing photo galleries from the last year (or two) and putting them online for your perusal. Vancouver 2010 Northland…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 5 comments