[Chicago] Can you write Perl in Python?

Ian Bicking ianb at colorstudy.com
Thu Dec 29 07:36:46 CET 2005


Michael Dudzik wrote:
> On Wed, 28 Dec 2005 19:17:11 -0600, "Ian Bicking" <ianb at colorstudy.com>
> said:
> 
>>I also noticed this works for saving another character:
>>
>>   [str(i)for i in(0,1,2)]
> 
> 
> 
> While we're saving each other bytes:
> 
>     [str(i)for i in 0,1,2]

For reasons that I think only become apparent when you starting thinking 
about weird nested of nested list comprehensions (which I haven't 
actually thought much about), this isn't allowed for generator 
comprehension, even if it is for list comprehension.  E.g.:

 >>> print list(i for i in(1,2,3))
[1, 2, 3]
 >>> print list([i for i in 1,2,3])
[1, 2, 3]
 >>> print list(i for i in 1,2,3)
   File "<stdin>", line 1
SyntaxError: invalid syntax

So the generator seems to save one character, even though it requires 
the parenthesis.

-- 
Ian Bicking  |  ianb at colorstudy.com  |  http://blog.ianbicking.org


More information about the Chicago mailing list