[Python-ideas] Assignments in list/generator expressions

M.-A. Lemburg mal at egenix.com
Tue Apr 12 12:05:49 CEST 2011


Georg Brandl wrote:
> On 12.04.2011 07:06, Nick Coghlan wrote:
>> On Tue, Apr 12, 2011 at 1:53 PM, Carl M. Johnson
>> <cmjohnson.mailinglist at gmail.com> wrote:
>>> On Mon, Apr 11, 2011 at 2:14 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>>>
>>>>  ys = [y for x in xs letting y = f(x) if y]
>>>
>>> Isn't that ambiguous? What if someone used the conditional expression:
>>> ys = [y for x in xs letting y = f(x) if y else g(x)] ? It seems like
>>> the letting/given has to go last in order to eliminate the possible
>>> ambiguity in the subsequent optional if clause.
>>
>> given/as resolves the parsing ambiguity problem by putting the names second:
>>
>> ys = [y for x in xs given f(x) if p(x) else g(x) as y]
> 
> Is it just me, or do others find this "string of clauses" (no matter what
> the actual keywords are) not very readable?

Same here.

If the "given:" blocks are only meant to hide away things in
a separate namespace, I'm not sure why we need a new keyword.

This is already possible using the "class" keyword and it even
gives the namespace a name ;-)

class myData:
  x = setup_x()
  y = setup_y()

z = myFormula(myData.x, myData.y)

That's a lot more readable, pythonic and intuitive than
the proposed "given:" block syntax, which -to me at least-
looks confusing.

BTW, I don't find the comparison to the list comprehension
syntax valid:

List comprehensions only allow
a very limited set of qualifiers which form the list
definition. As such, it makes sense to have the definition
behind the item expression (and it follows the rules used
in math for similar definitions).

The "given:" block, OTOH, makes no such restrictions
and may well contain code that doesn't have anything to
do with the statement it is supposed to configure. It has
the potential of introducing top-posting to Python code:

answer_to_question given:
  some_other_blurb
  original_question
  more_other_blurb

And things get even more exciting if you start to nest
these:

answer_to_original_question given:
  comment_to_answer_of_other_question given:
    answer_to_some_other_question given:
      other_question
    new_angle_to_discussion
  other_blurb
  original_question
  more_other_blurb

Following the flow of program execution and relevance
of the different parts to the final result can be
very confusing.

Also note that it is rather unusual and unexpected for
Python to execute the innermost block before the
outer one as it would happen in the above example.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Apr 12 2011)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-ideas mailing list