Is using range() in for loops really Pythonic?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Nagle

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

    Matt Nordhoff wrote:
    John Salerno wrote:
    >I know it's popular and very handy, but I'm curious if there are purists
    >out there who think that using something like:
    >>
    >for x in range(10):
    > #do something 10 times
    >>
    >is unPythonic. The reason I ask is because the structure of the for loop
    >seems to be for iterating through a sequence. It seems somewhat
    >artificial to use the for loop to do something a certain number of
    >times, like above.
    >>
    >Anyone out there refuse to use it this way, or is it just impossible to
    >avoid?
    >
    Well, you should use "xrange(10) " instead of "range(10)" .
    CPython really is naive. That sort of thing should be a
    compile-time optimization.

    John Nagle

    Comment

    • Ben Finney

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

      John Nagle <nagle@animats. comwrites:
      Matt Nordhoff wrote:
      Well, you should use "xrange(10) " instead of "range(10)" .
      >
      CPython really is naive. That sort of thing should be a
      compile-time optimization.
      Or even a case of the 'xrange' behaviour making 'range' obsolete.

      Which, as I pointed out earlier in the thread, it is in Python 3.0.

      --
      \ "Yesterday I parked my car in a tow-away zone. When I came back |
      `\ the entire area was missing." -- Steven Wright |
      _o__) |
      Ben Finney

      Comment

      • Marc 'BlackJack' Rintsch

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

        On Tue, 13 May 2008 10:20:41 -0700, John Nagle wrote:
        Matt Nordhoff wrote:
        >
        >Well, you should use "xrange(10) " instead of "range(10)" .
        >
        CPython really is naive. That sort of thing should be a
        compile-time optimization.
        It's not naive, it can't know at compile time what object is bound to the
        name `xrange` at runtime.

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • cokofreedom@gmail.com

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

          On May 14, 8:37 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
          On Tue, 13 May 2008 10:20:41 -0700, John Nagle wrote:
          Matt Nordhoff wrote:
          >
          Well, you should use "xrange(10) " instead of "range(10)" .
          >
          CPython really is naive. That sort of thing should be a
          compile-time optimization.
          >
          It's not naive, it can't know at compile time what object is bound to the
          name `xrange` at runtime.
          >
          Ciao,
          Marc 'BlackJack' Rintsch
          I think he meant you should just use xrange over range at all times.

          Comment

          • Graham Breed

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

            George Sakkis wrote:
            If you push this logic too far, you should del every name immediately
            after the last statement it is used in the scope. I would generally
            find less readable some code spread with del every few lines, micro-
            managing the effective scope of each name. YMMV.
            Yes, but ... how about

            for i in range(10):
            del i
            do stuff

            ?

            It makes it clear you aren't using the index and ensures you
            get a run-time error if you clobbered an existing variable.


            Graham

            Comment

            • Dave Parker

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

              Your point about for-loops was applicable not only to Python, but to
              many other programming languages. So in response, I've added two new
              for-loop variations to Flaming Thunder.

              The two new variations are for-forever-do and for-expression-times-do.
              For-forever allows you to explicitly create infinite loops, and for-
              expression-times allows you to do something a specific number of times
              without having to declare a looping variable if you don't need one.
              Examples:

              Write "Fa". For 8 times do write "-la".

              For forever do
              (
              Write "Do you know the definition of insanity? ".
              Read response.
              ).

              On May 10, 8:19 pm, John Salerno <johnj...@NOSPA Mgmail.comwrote :
              I know it's popular and very handy, but I'm curious if there are purists
              out there who think that using something like:
              >
              for x in range(10):
                  #do something 10 times
              >
              isunPythonic. The reason I ask is because the structure of the for loop
              seems to be for iterating through a sequence. It seems somewhat
              artificial to use the for loop to do something a certain number of
              times, like above.
              >
              Anyone out there refuse to use it this way, or is it just impossible to
              avoid?

              Comment

              Working...