[Python-Dev] For/while/if statements/comprehension/generator expressions unification

Alexander Myodov maa_public at sinn.ru
Tue Sep 20 01:16:27 CEST 2005


Hello,

  Well, we have "for", "while" and "if". We also have statements,
  list generator expressions and list comprehensions. In
  comprehensions we have the opportunity to use several for-s and in a
  same expression (for i in range (0, 640) for j in range (0, 640)),
  in loops we cannot; in comprehensions, we can use if-s to filter the
  items; in loops, we cannot, and we have to write an inner comparison instead.

  My opinion is that in so smart language as Python is, it would be
  great to have them generalized, so that the features available in
  list comprehensions should be available in statements, vice versa.
  All three statements (for/while/if) could be joined
  together into a single common one, where any of these could be
  combined and used multiple times (but for the cases when a
  "else"/"elif" etc causes are required, old-school one-statements
  should be left); and for such common expressions, they should be
  equal for both loop statements and list comprehensions/generator
  expressions.
  That is, the following loops should be possible:
  -------
  # This gives us some sugar to get rid of unnecessary indentations
  for x in range (0, 640) for y in range (0, 480):
  -------
  for x in range (0, 640) if should_handle_this_column(x) \
  for y in range (0, 480) if should_handle_this_row(y):
  -------
  for nX, nY in (
     f(x), f(y)
     for x in range (0, 640) if should_handle_this_column(x)
     for y in range (0, 480) if should_handle_this_row(y)
     ):
  -------
  for x in range (0, 640) for y in range (0, 480) while(!timeout()):
  -------
  for x in range (0, 640) while(!timeout_pos_in_row()) for y in range (0, 480)
while(!timeout_rows()):
  -------
  # And the latest and the hugest one:
  for x in range (0, 640) if should_handle_this_column(x) while(!timeout_pos_in_row()) \
  for y in range (0, 480) if should_handle_this_row(y) while(!timeout_rows() :
  -------
  # And almost the same as generator expression:
  for x, y in (
     f(x), f(y)
     for x in range (0, 640) if should_handle_this_column(x) while(!timeout_pos_in_row())
     for y in range (0, 480) if should_handle_this_row(y) while(!timeout_rows()
     )
  -------

  Hope I didn't miss something important...

-- 
With best regards,
 Alexander                          mailto:maa_public at sinn.ru




More information about the Python-Dev mailing list