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.