extend for loop syntax with if expr like listcomp&genexp ?

Discussion in 'Python' started by Bengt Richter, Jul 12, 2005.

  1. E.g., so we could write

    for x in seq if x is not None:
    print repr(x), "isn't None ;-)"

    instead of

    for x in (x for x in seq if x is not None):
    print repr(x), "isn't None ;-)"

    just a thought.

    Regards,
    Bengt Richter
    Bengt Richter, Jul 12, 2005
    #1
    1. Advertising

  2. Bengt Richter

    John Machin Guest

    Bengt Richter wrote:
    > E.g., so we could write
    >
    > for x in seq if x is not None:


    Chundrous; looks like that p**l language ...

    > print repr(x), "isn't None ;-)"
    >
    > instead of
    >
    > for x in (x for x in seq if x is not None):


    Byzantine ...

    > print repr(x), "isn't None ;-)"
    >
    > just a thought.
    >


    What's wrong with the following?

    for x in seq:
    if x is not None:
    print repr(x), "isn't None ;-)"
    John Machin, Jul 12, 2005
    #2
    1. Advertising

  3. Bengt Richter

    Ron Adam Guest

    Bengt Richter wrote:
    > E.g., so we could write
    >
    > for x in seq if x is not None:
    > print repr(x), "isn't None ;-)"
    >
    > instead of
    >
    > for x in (x for x in seq if x is not None):
    > print repr(x), "isn't None ;-)"
    >
    > just a thought.
    >
    > Regards,
    > Bengt Richter


    Is it new idea month? :)



    That would seem to follow the pattern of combining sequential lines that
    end in ':'.


    if pay<10 if hours>10 if stressed:
    sys.exit()

    That would be the same as using ands.



    And this gives us an if-try pattern with a shared else clause.

    if trapped try:
    exit = find('door')
    except:
    yell_for_help()
    else: #works for both if and try! ;-D
    leave()


    Which would be the same as:

    if trapped:
    try:
    exit = find('door')
    except:
    yell_for_help()
    else:
    leave()
    else:
    leave()


    Interesting idea, but I think it might make reading other peoples code
    more difficult.


    Cheers,
    Ron
    Ron Adam, Jul 12, 2005
    #3
  4. On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <> wrote:

    >Bengt Richter wrote:
    >> E.g., so we could write
    >>
    >> for x in seq if x is not None:

    >
    >Chundrous; looks like that p**l language ...

    ^^^^^^^^^--piqued my interest, where'd that come from? ;-)

    >
    >> print repr(x), "isn't None ;-)"
    >>
    >> instead of
    >>
    >> for x in (x for x in seq if x is not None):

    >
    >Byzantine ...

    Perhaps not if you wanted to enumerate the selected elements, as in
    for i, x in enumerate(x for x in seq if x is not None):
    >
    >> print repr(x), "isn't None ;-)"
    >>
    >> just a thought.
    >>

    >
    >What's wrong with the following?
    >
    >for x in seq:
    > if x is not None:
    > print repr(x), "isn't None ;-)"


    Nothing. Just noting that there's (at least) two kinds of for --
    the plain old one, and the ones inside list comprehensions and generator
    expressions, and it struck me that not allowing the full listcomp/genexp
    syntax in the ordinary for context was a seemingly unnecessary restriction.

    Regards,
    Bengt Richter
    Bengt Richter, Jul 12, 2005
    #4
  5. On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
    > On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <> wrote:
    > >Bengt Richter wrote:
    > >> for x in (x for x in seq if x is not None):

    > >Byzantine ...

    > Perhaps not if you wanted to enumerate the selected elements, as in
    > for i, x in enumerate(x for x in seq if x is not None):


    Seems like a bug waiting to happen -- wouldn't someone using that
    idiom most likely have *meant* something like this:

    for i,x in enumerate(seq):
    if x is not None:
    print "seq[%d] = %s is not None" % (i, repr(x))

    ?

    But of course that's not equivalent. It's hard to imagine a
    use case for an enumerated loop when the object being
    iterated over is anonymous (will be lost as soon as the loop
    exits).

    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
    Terry Hancock, Jul 13, 2005
    #5
  6. On Tue, 12 Jul 2005 23:07:07 -0500, Terry Hancock <> wrote:

    >On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
    >> On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <> wrote:
    >> >Bengt Richter wrote:
    >> >> for x in (x for x in seq if x is not None):
    >> >Byzantine ...

    >> Perhaps not if you wanted to enumerate the selected elements, as in
    >> for i, x in enumerate(x for x in seq if x is not None):

    >
    >Seems like a bug waiting to happen -- wouldn't someone using that
    >idiom most likely have *meant* something like this:
    >
    >for i,x in enumerate(seq):
    > if x is not None:
    > print "seq[%d] = %s is not None" % (i, repr(x))
    >
    >?
    >
    >But of course that's not equivalent. It's hard to imagine a
    >use case for an enumerated loop when the object being
    >iterated over is anonymous (will be lost as soon as the loop
    >exits).
    >

    Line numbers in a listing of non-None things?
    Page breaks at the right places?
    Filtering out '' instead of NOne from results of a string split before creating numbered
    html names for links to non-blank text elements in rendering text as html?
    I dunno, seems like at least a few possibilities for something halfway sensible...

    Regards,
    Bengt Richter
    Bengt Richter, Jul 13, 2005
    #6
  7. Bengt Richter

    Paul Rubin Guest

    Terry Hancock <> writes:
    > But of course that's not equivalent. It's hard to imagine a
    > use case for an enumerated loop when the object being
    > iterated over is anonymous (will be lost as soon as the loop exits).


    Huh? Not at all.

    print 'List of Python fans:'
    for i,x in enumerate([p for p in people if p.favorite_language == 'Python']):
    print '%d. %s'% (i, x.name)
    Paul Rubin, Jul 14, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boris Borcic

    genexp reduce syntax in 2.5a1

    Boris Borcic, Apr 18, 2006, in forum: Python
    Replies:
    0
    Views:
    277
    Boris Borcic
    Apr 18, 2006
  2. George Young
    Replies:
    7
    Views:
    255
    David Isaac
    Oct 4, 2006
  3. Emile van Sebille

    Odd listcomp behaviour

    Emile van Sebille, Dec 17, 2010, in forum: Python
    Replies:
    0
    Views:
    221
    Emile van Sebille
    Dec 17, 2010
  4. Good Night Moon
    Replies:
    9
    Views:
    265
    Rick DeNatale
    Jul 25, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page