Is using range() in for loops really Pythonic?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Illarionov

    #16
    Re: Is using range() in for loops really Pythonic?

    On Sun, 11 May 2008 18:52:48 -0700, Carl Banks wrote:
    On May 11, 6:44 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
    wrote:
    >In such cases, the name 'dummy' is conventionally bound to the items
    >from the iterator, for clarity of purpose::
    >>
    > for dummy in range(10):
    > # do stuff that makes no reference to 'dummy'
    >
    Is this documented? I've never heard of this convention. It's not PEP
    8, and I've never seen consistent usage of any name. I'd be interested
    in knowing where you read that this was a convention, or in what
    subcommunities it's a convention in.
    >
    I think dummy is a terrible name to use for this, since in no other
    usage I can think of does the word "dummy" suggest something isn't used.
    In fact, quite the opposite. For example, the dummy_threads module is
    most definitely used; the word dummy means that it's stripped down.
    Based on that, your usage of the symbol dummy above would suggest to me
    that it's a value used in lieu of something else (such as a calculated
    value).
    >
    In mathematics, a dummy argument another name for the independent
    variable of a function (or more accurately, the symbol used to represent
    it), which also doesn't match your usage.
    >
    If a value isn't used, then I think the most clear name for it is
    "unused".
    >
    >
    Carl Banks
    I agree with Carl. This group is the only place I've heard about this
    convension and it looks terrible IMO. Even if a value is not used, the
    variable still has a meaning: it is a counter, or an index.

    I know about the other convention: "for i in xrange" or "for i in range".
    Wikipedia agrees with me:

    Examples in wikipedia article use either "i" or "counter".

    It is established convention in almost every programming language and its
    origins are in mathematics (matrix indices).

    Google search for "for dummy in range" (with quotes) gives 164 results
    While "for dummy in range" gives 146 000 results. Almost thousand times
    more.

    So, "i" is more conventional and as such is more readable.

    -- Ivan

    Comment

    • Ben Finney

      #17
      Convention for name indicating &quot;don't care about the value&quot; (was: Is using range() in for loops really Pythonic?)

      Carl Banks <pavlovevidence @gmail.comwrite s:
      On May 11, 6:44 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
      wrote:
      In such cases, the name 'dummy' is conventionally bound to the items
      from the iterator, for clarity of purpose::

      for dummy in range(10):
      # do stuff that makes no reference to 'dummy'
      >
      Is this documented?
      It's not a documented standard, to my knowledge.
      I've never heard of this convention. It's not PEP 8, and I've never
      seen consistent usage of any name. I'd be interested in knowing
      where you read that this was a convention, or in what subcommunities
      it's a convention in.
      It has been in this forum that the use of the name '_' for "don't care
      about the value" was deprecated, since that name is already overloaded
      with other meanings.
      I think dummy is a terrible name to use for this, since in no other
      usage I can think of does the word "dummy" suggest something isn't
      used.
      I think it's far superior to '_'. I'd be just as happy with any other
      name that explicitly distinguishes itself for this purpose.
      If a value isn't used, then I think the most clear name for it is
      "unused".
      Sounds good to me. Now we merely need to convince the world.

      --
      \ “Software patents provide one more means of controlling access |
      `\ to information. They are the tool of choice for the internet |
      _o__) highwayman.” —Anthony Taylor |
      Ben Finney

      Comment

      • John Salerno

        #18
        Re: Is using range() in for loops really Pythonic?

        Ben Finney wrote:
        John Salerno <johnjsal@gmail NOSPAM.comwrite s:
        >
        >num = 33
        >>
        >for x in xrange(10):
        > print num += 1
        >
        Which is better done by 'num += 10'.
        >
        Can you come up with an example that isn't trivially replaced with
        clearer code? That might make it clearer what your concern is.
        ::sigh:: No, unfortunately I don't have a strong enough grasp of Python
        to give a really in-depth example. I understand what you're asking
        though. Perhaps people don't use this idiom as much as I think they do,
        so to give a trivial example to support my point isn't helpful.

        As far as I know, though, I think this is used often enough, so I
        thought I'd just ask if there are purists out there who don't like this
        use of the loop and feel it's an abuse of the syntax.

        Comment

        • Carl Banks

          #19
          Re: Convention for name indicating &quot;don't care about the value&quot; (was:Is using range() in for loops really Pythonic?)

          On May 11, 11:41 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
          wrote:
          Carl Banks <pavlovevide... @gmail.comwrite s:
          On May 11, 6:44 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
          wrote:
          In such cases, the name 'dummy' is conventionally bound to the items
          from the iterator, for clarity of purpose::
          >
          for dummy in range(10):
          # do stuff that makes no reference to 'dummy'
          >
          Is this documented?
          >
          It's not a documented standard, to my knowledge.
          >
          I've never heard of this convention. It's not PEP 8, and I've never
          seen consistent usage of any name. I'd be interested in knowing
          where you read that this was a convention, or in what subcommunities
          it's a convention in.
          >
          It has been in this forum that the use of the name '_' for "don't care
          about the value" was deprecated, since that name is already overloaded
          with other meanings.
          That doesn't follow at all from what you wrote: no one in this thread
          had even mentioned the usage of "_" for unused values--how did you
          expect us to know you were talking about something no one brought up?

          So it seems that usage of "dummy" is not a convention, and avoidance
          of "_" is not really a convention except on this group.

          I think dummy is a terrible name to use for this, since in no other
          usage I can think of does the word "dummy" suggest something isn't
          used.
          >
          I think it's far superior to '_'. I'd be just as happy with any other
          name that explicitly distinguishes itself for this purpose.
          >
          If a value isn't used, then I think the most clear name for it is
          "unused".
          >
          Sounds good to me. Now we merely need to convince the world.
          I'm happy to discourage the world from using "_" for this purpose;
          ambivalent whether you even need to use a specific name.


          Carl Banks

          Comment

          • Ivan Illarionov

            #20
            Re: Is using range() in for loops really Pythonic?

            >In such cases, the name 'dummy' is conventionally bound to the items
            >from the iterator, for clarity of purpose
            [..]
            If a value isn't used, then I think the most clear name for it is
            "unused".
            [...]

            Maybe my brain works differently, but I find both "dummy" and "unused"
            are extremely confusing names for loop counters. The loop begins to look
            like it doesn't iterate at all if its counter is dummy or unused.

            If it *counts* it is *used* and it's *not* dummy.

            Why reinvent the wheel when "a common identifier naming convention is for
            the loop counter to use the variable names i, j and k (and so on if
            needed)" (from Wikipedia http://en.wikipedia.org/wiki/Loop_counter )

            -- Ivan

            Comment

            • Ben Finney

              #21
              Re: Is using range() in for loops really Pythonic?

              John Salerno <johnjsal@NOSPA Mgmail.comwrite s:
              Ben Finney wrote:
              >
              John Salerno <johnjsal@gmail NOSPAM.comwrite s:
              num = 33
              >
              for x in xrange(10):
              print num += 1
              Which is better done by 'num += 10'.

              Can you come up with an example that isn't trivially replaced with
              clearer code? That might make it clearer what your concern is.
              >
              ::sigh:: No, unfortunately I don't have a strong enough grasp of
              Python to give a really in-depth example.
              It need not be in-depth, merely illustrative of the problem being
              addressed.
              I understand what you're asking though. Perhaps people don't use
              this idiom as much as I think they do, so to give a trivial example
              to support my point isn't helpful.
              I think that the idiom

              for unused in xrange(10):
              # do stuff with no reference to 'unused'

              is quite common. Is that what you're asking about?
              As far as I know, though, I think this is used often enough, so I
              thought I'd just ask if there are purists out there who don't like
              this use of the loop and feel it's an abuse of the syntax.
              No, it seems quite a normal usage of the syntax and a good use of an
              iterator.

              With "do something N times", there must be *something* to keep track
              of which iteration we're up to (or, equivalently, how many iterations
              remain) at a given moment. A Python iterator seems a fine choice to
              hold that information, and better than many alternatives.

              --
              \ "My house is on the median strip of a highway. You don't really |
              `\ notice, except I have to leave the driveway doing 60 MPH." -- |
              _o__) Steven Wright |
              Ben Finney

              Comment

              • Ben Finney

                #22
                Re: Is using range() in for loops really Pythonic?

                Ivan Illarionov <ivan.illariono v@gmail.comwrit es:
                In such cases, the name 'dummy' is conventionally bound to the items
                from the iterator, for clarity of purpose
                [..]
                If a value isn't used, then I think the most clear name for it is
                "unused".
                [...]
                >
                Maybe my brain works differently, but I find both "dummy" and
                "unused" are extremely confusing names for loop counters. The loop
                begins to look like it doesn't iterate at all if its counter is
                dummy or unused.
                >
                If it *counts* it is *used* and it's *not* dummy.
                The value is unused by any of the code inside the block. For the
                purposes of that block, it is a dummy value.

                That something *else* (the iterator driving the 'for') is taking care
                of knowing when the loop is finished is great. However, in the code
                the programmer is writing, the loop counter is entirely unused.
                Why reinvent the wheel when "a common identifier naming convention
                is for the loop counter to use the variable names i, j and k (and so
                on if needed)" (from Wikipedia
                http://en.wikipedia.org/wiki/Loop_counter )
                That is also regrettably common in Python code. It still suffers from
                being unnecessarily ambiguous, since there are *also* plenty of loops
                using 'i', 'j', etc. where the loop counter *is* used.

                Differentiating these use cases by appropriate naming is, IMO, worth
                the effort of choosing a meaningful name.

                --
                \ "Ignorance more frequently begets confidence than does |
                `\ knowledge." —Charles Darwin, _The Descent of Man_, 1871 |
                _o__) |
                Ben Finney

                Comment

                • Paddy

                  #23
                  Re: Is using range() in for loops really Pythonic?

                  On May 11, 9:28 pm, Arnaud Delobelle <arno...@google mail.comwrote:
                  Hi John, Arnaud;
                  Contrived example:
                  >
                  # Print 'hello' 10 times; x is not used
                  for x in xrange(10):
                  print 'hello'
                  I've used Fortran and C and so would tend to use either i,j,k as the
                  unused loop variable above, or, for clarity, call it something
                  descriptive like loop_count, if the loop body would be clearer.

                  I would also just use range for small, (especially small constant),
                  ranges.
                  >
                  # By changing what is iterated over, no unused variable:
                  from itertools import repeat
                  for msg in repeat('hello', 10):
                  print msg
                  I guess I would not go to the trouble of using itertools.repea t unless
                  it was simplifying/homogenising a larger expression - i.e if it was
                  part of some functional expression

                  I've never fealt the need for a separate repeat statement myself
                  either.

                  - Paddy.

                  P.S: I do understand that your examples are contrived. Just my
                  thoughts.

                  Comment

                  • Ivan Illarionov

                    #24
                    Re: Is using range() in for loops really Pythonic?

                    On Mon, 12 May 2008 16:24:23 +1000, Ben Finney wrote:
                    [...]
                    That is also regrettably common in Python code. It still suffers from
                    being unnecessarily ambiguous, since there are *also* plenty of loops
                    using 'i', 'j', etc. where the loop counter *is* used.
                    >
                    Differentiating these use cases by appropriate naming is, IMO, worth the
                    effort of choosing a meaningful name.
                    Even if the counter is not used inside the loop's body it's still in
                    control of the whole loop and, IMO, any special discriminating and non-
                    conventional name doesn't worth the added confusion.

                    -- Ivan

                    Comment

                    • Roel Schroeven

                      #25
                      Re: Is using range() in for loops really Pythonic?

                      John Salerno schreef:
                      Ben Finney wrote:
                      >
                      >John Salerno <johnjsal@gmail NOSPAM.comwrite s:
                      >>
                      >>num = 33
                      >>>
                      >>for x in xrange(10):
                      >> print num += 1
                      >Which is better done by 'num += 10'.
                      >>
                      >Can you come up with an example that isn't trivially replaced with
                      >clearer code? That might make it clearer what your concern is.
                      >
                      ::sigh:: No, unfortunately I don't have a strong enough grasp of Python
                      to give a really in-depth example. I understand what you're asking
                      though. Perhaps people don't use this idiom as much as I think they do,
                      so to give a trivial example to support my point isn't helpful.
                      Perhaps a function to print some text a number of times:

                      def print_n(text, n):
                      for i in xrange(n):
                      print text

                      (Though I guess you can replace that with print '\n'.join(msg * n) )

                      --
                      The saddest aspect of life right now is that science gathers knowledge
                      faster than society gathers wisdom.
                      -- Isaac Asimov

                      Roel Schroeven

                      Comment

                      • Ben Finney

                        #26
                        Re: Is using range() in for loops really Pythonic?

                        Ivan Illarionov <ivan.illariono v@gmail.comwrit es:
                        On Mon, 12 May 2008 16:24:23 +1000, Ben Finney wrote:
                        [...]
                        That is also regrettably common in Python code. It still suffers
                        from being unnecessarily ambiguous, since there are *also* plenty
                        of loops using 'i', 'j', etc. where the loop counter *is* used.

                        Differentiating these use cases by appropriate naming is, IMO,
                        worth the effort of choosing a meaningful name.
                        >
                        Even if the counter is not used inside the loop's body it's still in
                        control of the whole loop
                        Not in Python it's not. The values that come out of the iterator
                        aren't "in control of the loop". The state for the loop is in the
                        *iterator*, not the values that come out.

                        Having access to the values that come from the iterator is usually
                        useful, but regardless of whether one uses them or not, they're *not*
                        controlling the loop, and it's confusing to imply that they are.

                        So, when not using the values that come from the controlling iterator,
                        it's good to make that explicit. If Python supported it, we might
                        prefer to use no name at all for something that isn't used, but the
                        'for' syntax doesn't allow that.

                        In the absence of supporting syntax, the next best thing is to choose
                        a name that *does* make it explicit that the values will not be used.

                        --
                        \ “All television is educational television. The question is: |
                        `\ what is it teaching?” —Nicholas Johnson |
                        _o__) |
                        Ben Finney

                        Comment

                        • Ben Finney

                          #27
                          Re: Is using range() in for loops really Pythonic?

                          Paddy <paddy3118@goog lemail.comwrite s:
                          I've used Fortran and C and so would tend to use either i,j,k as the
                          unused loop variable above, or, for clarity, call it something
                          descriptive like loop_count, if the loop body would be clearer.
                          The problem with all of these names is that they also have long
                          precedent as names of values that *will* be used inside the loop.

                          Because of the precedent of those names, choosing one of those names
                          doesn't make it clear to the reader that the value is never used; they
                          have no indication from you of that until they look over the code a
                          few times. It's implicit rather than explicit.

                          --
                          \ "As I bit into the nectarine, it had a crisp juiciness about it |
                          `\ that was very pleasurable - until I realized it wasn't a |
                          _o__) nectarine at all, but A HUMAN HEAD!!" -- Jack Handey |
                          Ben Finney

                          Comment

                          • Ivan Illarionov

                            #28
                            Re: Is using range() in for loops really Pythonic?

                            >On Mon, 12 May 2008 16:24:23 +1000, Ben Finney wrote: [...]
                            That is also regrettably common in Python code. It still suffers from
                            being unnecessarily ambiguous, since there are *also* plenty of loops
                            using 'i', 'j', etc. where the loop counter *is* used.
                            >
                            Differentiating these use cases by appropriate naming is, IMO, worth
                            the effort of choosing a meaningful name.
                            >>
                            >Even if the counter is not used inside the loop's body it's still in
                            >control of the whole loop
                            >
                            Not in Python it's not. The values that come out of the iterator aren't
                            "in control of the loop". The state for the loop is in the *iterator*,
                            not the values that come out.
                            >
                            Having access to the values that come from the iterator is usually
                            useful, but regardless of whether one uses them or not, they're *not*
                            controlling the loop, and it's confusing to imply that they are.
                            I agree that "in control" was incorrect phrase.
                            So, when not using the values that come from the controlling iterator,
                            it's good to make that explicit. If Python supported it, we might prefer
                            to use no name at all for something that isn't used, but the 'for'
                            syntax doesn't allow that.
                            >
                            In the absence of supporting syntax, the next best thing is to choose a
                            name that *does* make it explicit that the values will not be used.
                            Name 'i' does not imply whether it's used inside the loop or not.
                            IMO it perfectly covers both cases.

                            It may have small advantage to "make it explicit that the values will not
                            be used", but names like "dummy", "unused", "ignored" or "junk" can be
                            confusing for some people and break the established conventions for
                            counter variable names (and these conventions are reasonable and are
                            taken from mathematics). "i" is still the best choice.

                            And let's agree to disagree. It's clear that we have different opinions
                            and it looks that this discussion is not going to change them.

                            -- Ivan

                            Comment

                            • Grant Edwards

                              #29
                              Re: Is using range() in for loops really Pythonic?

                              On 2008-05-12, Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
                              >Maybe my brain works differently, but I find both "dummy" and
                              >"unused" are extremely confusing names for loop counters. The loop
                              >begins to look like it doesn't iterate at all if its counter is
                              >dummy or unused.
                              >>
                              >If it *counts* it is *used* and it's *not* dummy.
                              >
                              The value is unused by any of the code inside the block. For the
                              purposes of that block, it is a dummy value.
                              The value may be unused, but for me it's the name that matters,
                              not the value. The name might be in use by other code, and
                              the careless choice of a "dummy" name that's _supposed_ to be
                              unused has broken code precisely becuase the name was being
                              used (for something else). Requiring that the user pollute a
                              namespace with a useless name is a wart.
                              That is also regrettably common in Python code. It still
                              suffers from being unnecessarily ambiguous, since there are
                              *also* plenty of loops using 'i', 'j', etc. where the loop
                              counter *is* used.
                              Perhaps I'm the only one who's ever been stupid enough to
                              overwrite an index named "i" (that is being used) with another
                              index named "i" (that isn't being used)...


                              --
                              Grant Edwards grante Yow! All of life is a blur
                              at of Republicans and meat!
                              visi.com

                              Comment

                              • Grant Edwards

                                #30
                                Re: Is using range() in for loops really Pythonic?

                                On 2008-05-12, Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:

                                I too, agree that requiring a name be bound to the values
                                coming out of the iterator seems "wrong".
                                With "do something N times", there must be *something* to keep track
                                of which iteration we're up to (or, equivalently, how many iterations
                                remain) at a given moment. A Python iterator seems a fine choice to
                                hold that information, and better than many alternatives.
                                An iterator like xrange() is an excellent choice. But, since
                                the iterator contains that information, why require that that
                                value be "exported" by the iterator and bound to an externally
                                visible name? In the case in question, the only thing you need
                                from the iterator is the StopIteration exception. To me,
                                exposing the internal state of the iterator and requiring that
                                the user bind a name to it each time through the loop feels
                                we're like driving a nail with a screwdriver.

                                --
                                Grant Edwards grante Yow! Was my SOY LOAF left
                                at out in th'RAIN? It tastes
                                visi.com REAL GOOD!!

                                Comment

                                Working...