x[i] as loop variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gerrit Holl

    x[i] as loop variable

    Hi,

    PEP 289 says:
    (Loop variables may also use constructs like x[i] or x.a; this form may be deprecated.)

    What is this about? I found this:
    19:41:33:232:35 >>> a=Test()
    19:41:37:232:36 >>> for a.foo in L: print a.foo
    19:41:37:232:36 ...
    <__main__.Tes t object at 0x4018516c>
    <__main__.Tes t object at 0x401854cc>
    <__main__.Tes t object at 0x401854ac>
    19:41:49:232:37 >>> for obj in L: print obj.foo,
    19:41:49:232:37 ...
    bar baz bax

    What is the use of this? When should I use it? Why 'may it be deprecated'?
    I don't see this construct in the Language Reference. I looked at:


    yours probably,
    Gerrit.

    --
    that he caught run away from him, then shall he swear
    to the owners of the slave, and he is free of all blame.
    -- 1780 BC, Hammurabi, Code of Law
    --
    Asperger Syndroom - een persoonlijke benadering:

    Kom in verzet tegen dit kabinet:
    De SP kiest voor menselijke waardigheid, gelijkwaardigheid en solidariteit. Sluit je aan bij de sociaalste en actiefste partij van Nederland!


  • Raymond Hettinger

    #2
    Re: x[i] as loop variable

    [Gerrit Holl][color=blue]
    > PEP 289 says:
    > (Loop variables may also use constructs like x[i] or x.a; this form may be[/color]
    deprecated.)

    That was taken out of the pep because it is a distractor issue.

    [color=blue]
    > What is the use of this? When should I use it?[/color]

    In general, it should never be used unless you're trying to obfuscate
    your code. It is a long standing, unintended by-product of the
    grammar that any lvalue can be used as a loop variable. Most folks
    aren't aware of it and everyone is better off not knowing about it.
    In fact, I wish it hadn't come-up at all because someone will read
    about it and adopt the atrocity as their new favorite style.

    So, close your eyes, forget this note, and go read about
    something interesting and useful.


    Raymond Hettinger



    Comment

    • Just

      #3
      Re: x[i] as loop variable

      In article <yRWlb.17437$Fc 5.1106@nwrdny01 .gnilink.net>,
      "Raymond Hettinger" <vze4rx4y@veriz on.net> wrote:
      [color=blue]
      > [Gerrit Holl][color=green]
      > > PEP 289 says:
      > > (Loop variables may also use constructs like x[i] or x.a; this form may be[/color]
      > deprecated.)
      >
      > That was taken out of the pep because it is a distractor issue.
      >
      >[color=green]
      > > What is the use of this? When should I use it?[/color]
      >
      > In general, it should never be used unless you're trying to obfuscate
      > your code. It is a long standing, unintended by-product of the
      > grammar that any lvalue can be used as a loop variable. Most folks
      > aren't aware of it and everyone is better off not knowing about it.
      > In fact, I wish it hadn't come-up at all because someone will read
      > about it and adopt the atrocity as their new favorite style.[/color]

      But isn't the feature a neccesity to make this work:

      for a, b in zip(seqA, seqB):
      ...

      ?

      Just

      Comment

      • Raymond Hettinger

        #4
        Re: x[i] as loop variable

        [Just][color=blue]
        > But isn't the feature a neccesity to make this work:
        >
        > for a, b in zip(seqA, seqB):[/color]


        Yes, that is why it is there to begin with.
        But that doesn't mean going off the deep-end and writing:

        for a[3] in data: . . .
        for a[:] in data: . . .
        for a.abomination in data: . . .
        for d[k] in data: . . .


        Raymond Hettinger


        Comment

        • Erik Max Francis

          #5
          Re: x[i] as loop variable

          Raymond Hettinger wrote:
          [color=blue]
          > In general, it should never be used unless you're trying to obfuscate
          > your code. It is a long standing, unintended by-product of the
          > grammar that any lvalue can be used as a loop variable. Most folks
          > aren't aware of it and everyone is better off not knowing about it.
          > In fact, I wish it hadn't come-up at all because someone will read
          > about it and adopt the atrocity as their new favorite style.[/color]

          Yes, I encountered this when implementing iterative control markups for
          EmPy. I was quite surprised that such syntaxes were allowed in Python;
          I'd never seen them or heard of them being used, and only through an
          experiment did I realize much to my surprise that, as you say, any
          lvalue can be used as the iteration variable.

          Even though the EmPy control markups are intended to mimic the native
          Python control structures as closely as possible (the pinciple of least
          surprise), needless to say, I got no complaints about deliberately
          deviating from Python in this respect; in EmPy, iteration variables must
          be simple variable names and nothing more.

          --
          Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
          / \ Pick the roses from the thorns / Wash with waters of the storms
          \__/ Chante Moore

          Comment

          • Erik Max Francis

            #6
            Re: x[i] as loop variable

            Just wrote:
            [color=blue]
            > But isn't the feature a neccesity to make this work:
            >
            > for a, b in zip(seqA, seqB):
            > ...
            >
            > ?[/color]

            I would guess that's _why_ the "feature" is there, but you can certainly
            have one without the other. To use the EmPy control markup example
            again (which is, as I mentioned in another post, intended to mimic the
            native Python control structures as closely as is reasonably possible),
            I support tuple iteration variables (e.g., @[for x, y in seq]) -- even
            hierarchically -- but not the more generalized lvalues (@[for a[i] in
            seq] or @[for a.x in seq] are errors).

            I would submit that in Python, the latter examples came along as excess
            baggage for the purpose for supporting tuple variables (which is indeed
            a very useful feature, and thus is supported in EmPy). I can't imagine
            anyone actually using such things deliberately except to be difficult.

            --
            Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
            __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
            / \ Pick the roses from the thorns / Wash with waters of the storms
            \__/ Chante Moore

            Comment

            • Alex Martelli

              #7
              Re: x[i] as loop variable

              Raymond Hettinger wrote:
              [color=blue]
              > But that doesn't mean going off the deep-end and writing:
              >
              > for a[3] in data: . . .
              > for a[:] in data: . . .
              > for a.abomination in data: . . .
              > for d[k] in data: . . .[/color]

              Still, there IS some "coolness factor" in:
              [color=blue][color=green][color=darkred]
              >>> d = {}
              >>> for k, d[k] in pairs: pass[/color][/color][/color]
              ....[color=blue][color=green][color=darkred]
              >>> d[/color][/color][/color]
              {'p': 'i', 'k': 'o', 'b': 'a', 'm': 'a', 'l': 'u'}

              after all, "for k, d[k] in pairs: pass" is so much more
              show-offy than a mere "d.update(dict( pairs))"...!-)


              Alex

              Comment

              Working...