• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • What is in hindsight a foolish way to do generator expressions

    March 5th, 2009
    programming, python  [html]
    In python I often want to do things like this:
           if x.startswith("foo") for all x in a_list:
              ...
    
           if is_verb(leaf) for some leaf in a_tree:
              ...
        
    I usually end up coding these a bunch of different ways, the shortest of which are:
            if False not in [x.startswith("foo") for x in a_list]:
               ...
    
            if True in [is_verb(leaf) for leaf in a_tree]:
               ...
        
    But these don't short circut and are ugly. Sometimes I'll write little funtions but I don't like that either. Currently the syntax if [expr] for ... doesn't mean anything, so this should be possible. I wonder if it would be worth writing a PEP.

    Update 3/6/2009: George Dahl writes that python already does this. I should be writing those examples as:

           if all(x.startswith("foo") for x in a_list):
              ...
    
           if any(is_verb(leaf) for leaf in a_tree):
              ...
        
    These examples use generator expressions instead of list comprehensions. And builtins any and all. Especially as one can use any function, not just any or all, this is a much nicer syntax than what I wanted. It doesn't read as fluidly as english, but then again it doesn't deal with these two specific case as special cases of the if statement. So I'm quite happy.

    The only other real downside is also I think the reason I didn't end up with this when fiddling around at the python prompt: you need somewhat non-intuitive parens. I'm pretty sure when I was trying to see if there was a way to do what I wanted I did:

        >>> x for x in range(7)
          File "<stdin>", line 1
            x for x in range(7)
                ^
          SyntaxError: invalid syntax
        
    And then moved on. As a human I don't see an obvious reason why I can't enter the obvious thing. Is there something else a statement beginning with "x for " could mean?

    Comment via: facebook

    Recent posts on blogs I like:

    Streaming the Biden Infrastructure Plan

    I streamed my thoughts about the Biden infrastructure plan, and unlike previous streams, I uploaded this to YouTube. I go into more details (and more tangents) on video, but, some key points: Out of the nearly $600 billion in the current proposal that is …

    via Pedestrian Observations April 11, 2021

    Collections: Clothing, How Did They Make it? Part IVb: Cloth Money

    This is the second half of the fourth part of our four part (I, II, III, IVa) look at the production of textiles, particularly wool and linen, in the pre-modern world. Last time, we looked at commercial textile workers and the finishing processes for text…

    via A Collection of Unmitigated Pedantry April 9, 2021

    Notes from “Don’t Shoot the Dog”

    I just finished Karen Pryor’s “Don’t Shoot the Dog: the New Art of Teaching and Training.” Partly because a friend points out that it’s not on Audible and therefore she can’t possibly read it, here are the notes I took and some thoughts. It’s a quick, eas…

    via The whole sky April 2, 2021

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact