Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

Paul Rubin no.email at nospam.invalid
Thu Feb 18 22:58:21 EST 2010


Steve Howell <showell30 at yahoo.com> writes:

>> But frankly, although there's no reason that you _have_ to name the
>> content at each step, I find it a lot more readable if you do:
>>
>> def print_numbers():
>>     tuples = [(n*n, n*n*n) for n in (1,2,3,4,5,6)]
>>     filtered = [ cube for (square, cube) in tuples if square!=25 and
>> cube!=64 ]
>>     for f in filtered:
>>         print f
>
> The names you give to the intermediate results here are
> terse--"tuples" and "filtered"--so your code reads nicely.

But that example makes tuples and filtered into completely expanded
lists in memory.  I don't know Ruby so I've been wondering whether the
Ruby code would run as an iterator pipeline that uses constant memory.

> In a more real world example, the intermediate results would be
> something like this:
>
>    departments
>    departments_in_new_york
>    departments_in_new_york_not_on_bonus_cycle
>    employees_in_departments_in_new_york_not_on_bonus_cycle
>    names_of_employee_in_departments_in_new_york_not_on_bonus_cycle

http://haskell.org/ghc/docs/6.10.4/html/users_guide/syntax-extns.html#generalised-list-comprehensions

might be of interest.  Maybe Ruby and/or Python could grow something similar.



More information about the Python-list mailing list