Easy way to keep counting up infinitely











up vote
5
down vote

favorite












What's a good way to keep counting up infinitely? I'm trying to write a condition that will keep going until there's no value in a database, so it's going to iterate from 0, up to theoretically infinity (inside a try block, of course).



How would I count upwards infinitely? Or should I use something else?



I am looking for something similar to i++ in other languages, where it keeps iterating until failure.










share|improve this question






















  • Also, why doesn't Python have i++? Literally every other language I've worked in does, and it seems like a glaring hole.
    – Andrew Alexander
    Jul 11 '12 at 2:49






  • 6




    1) i++ is syntactic sugar and I doubt that you can call it's exclusion a "glaring hole". 2) Python uses i += 1 because it is more explicit about the increment. 3) Guido decided it was so.
    – Joel Cornett
    Jul 11 '12 at 2:51






  • 2




    i+=1 is only one more character
    – user485498
    Jul 11 '12 at 2:51






  • 4




    @AndrewAlexander "There should be one-- and preferably only one --obvious way to do it."
    – jamylak
    Jul 11 '12 at 2:53

















up vote
5
down vote

favorite












What's a good way to keep counting up infinitely? I'm trying to write a condition that will keep going until there's no value in a database, so it's going to iterate from 0, up to theoretically infinity (inside a try block, of course).



How would I count upwards infinitely? Or should I use something else?



I am looking for something similar to i++ in other languages, where it keeps iterating until failure.










share|improve this question






















  • Also, why doesn't Python have i++? Literally every other language I've worked in does, and it seems like a glaring hole.
    – Andrew Alexander
    Jul 11 '12 at 2:49






  • 6




    1) i++ is syntactic sugar and I doubt that you can call it's exclusion a "glaring hole". 2) Python uses i += 1 because it is more explicit about the increment. 3) Guido decided it was so.
    – Joel Cornett
    Jul 11 '12 at 2:51






  • 2




    i+=1 is only one more character
    – user485498
    Jul 11 '12 at 2:51






  • 4




    @AndrewAlexander "There should be one-- and preferably only one --obvious way to do it."
    – jamylak
    Jul 11 '12 at 2:53















up vote
5
down vote

favorite









up vote
5
down vote

favorite











What's a good way to keep counting up infinitely? I'm trying to write a condition that will keep going until there's no value in a database, so it's going to iterate from 0, up to theoretically infinity (inside a try block, of course).



How would I count upwards infinitely? Or should I use something else?



I am looking for something similar to i++ in other languages, where it keeps iterating until failure.










share|improve this question













What's a good way to keep counting up infinitely? I'm trying to write a condition that will keep going until there's no value in a database, so it's going to iterate from 0, up to theoretically infinity (inside a try block, of course).



How would I count upwards infinitely? Or should I use something else?



I am looking for something similar to i++ in other languages, where it keeps iterating until failure.







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 11 '12 at 2:43









Andrew Alexander

2,9852075135




2,9852075135












  • Also, why doesn't Python have i++? Literally every other language I've worked in does, and it seems like a glaring hole.
    – Andrew Alexander
    Jul 11 '12 at 2:49






  • 6




    1) i++ is syntactic sugar and I doubt that you can call it's exclusion a "glaring hole". 2) Python uses i += 1 because it is more explicit about the increment. 3) Guido decided it was so.
    – Joel Cornett
    Jul 11 '12 at 2:51






  • 2




    i+=1 is only one more character
    – user485498
    Jul 11 '12 at 2:51






  • 4




    @AndrewAlexander "There should be one-- and preferably only one --obvious way to do it."
    – jamylak
    Jul 11 '12 at 2:53




















  • Also, why doesn't Python have i++? Literally every other language I've worked in does, and it seems like a glaring hole.
    – Andrew Alexander
    Jul 11 '12 at 2:49






  • 6




    1) i++ is syntactic sugar and I doubt that you can call it's exclusion a "glaring hole". 2) Python uses i += 1 because it is more explicit about the increment. 3) Guido decided it was so.
    – Joel Cornett
    Jul 11 '12 at 2:51






  • 2




    i+=1 is only one more character
    – user485498
    Jul 11 '12 at 2:51






  • 4




    @AndrewAlexander "There should be one-- and preferably only one --obvious way to do it."
    – jamylak
    Jul 11 '12 at 2:53


















Also, why doesn't Python have i++? Literally every other language I've worked in does, and it seems like a glaring hole.
– Andrew Alexander
Jul 11 '12 at 2:49




Also, why doesn't Python have i++? Literally every other language I've worked in does, and it seems like a glaring hole.
– Andrew Alexander
Jul 11 '12 at 2:49




6




6




1) i++ is syntactic sugar and I doubt that you can call it's exclusion a "glaring hole". 2) Python uses i += 1 because it is more explicit about the increment. 3) Guido decided it was so.
– Joel Cornett
Jul 11 '12 at 2:51




1) i++ is syntactic sugar and I doubt that you can call it's exclusion a "glaring hole". 2) Python uses i += 1 because it is more explicit about the increment. 3) Guido decided it was so.
– Joel Cornett
Jul 11 '12 at 2:51




2




2




i+=1 is only one more character
– user485498
Jul 11 '12 at 2:51




i+=1 is only one more character
– user485498
Jul 11 '12 at 2:51




4




4




@AndrewAlexander "There should be one-- and preferably only one --obvious way to do it."
– jamylak
Jul 11 '12 at 2:53






@AndrewAlexander "There should be one-- and preferably only one --obvious way to do it."
– jamylak
Jul 11 '12 at 2:53














4 Answers
4






active

oldest

votes

















up vote
20
down vote



accepted










Take a look at itertools.count().



From the docs:




count(start=0, step=1) --> count object



Make an iterator that returns evenly spaced values starting with n.
Equivalent to:




def count(start=0, step=1):
# count(10) --> 10 11 12 13 14 ...
# count(2.5, 0.5) -> 2.5 3.0 3.5 ...
n = start
while True:
yield n
n += step


So for example:



import itertools

for i in itertools.count(13):
print(i)


would generate an infinite sequence starting with 13, in steps of +1. And, I hadn't tried this before, but you can count down too of course:



for i in itertools.count(100, -5):
print(i)


starts at 100, and keeps subtracting 5 for each new value ....









share|improve this answer



















  • 1




    Beat me to it...
    – jamylak
    Jul 11 '12 at 2:45






  • 1




    @jamylak got lucky:)
    – Levon
    Jul 11 '12 at 2:45










  • I just couldn't stand the SO auto code formatter incorrect colors :)
    – jamylak
    Jul 11 '12 at 3:38


















up vote
0
down vote













This is a bit smaller code than what the other user provided!



x = 1
while True:
x = x+1
print x





share|improve this answer























  • I like count but this is also valid and can come in handy if you need multiple counters. Would update to x += 1
    – CasualDemon
    Oct 5 '17 at 21:04


















up vote
0
down vote













A little shorter, using an iterator and no library:



x = 0
for x in iter(lambda: x+1, -1):
print(x)


But it requires a variable in the current scope.






share|improve this answer




























    up vote
    -1
    down vote













    xrange will give you an iterable object that won't use memory as you iterate, a for loop is cleaner than a while loop + counter if you ask me.



    import sys
    for i in xrange(0,sys.maxint):
    print i





    share|improve this answer























    • Not really infinite because it stops at sys.maxint, after which the domain of the long integers start automagically.
      – Eric
      Nov 10 at 23:37










    • Not infinite, but "similar to i++ in other languages" though. And of course x=0, 1, 2... will overflow at the same point that this fails anyway, so the solution you have posed has the same limitation effectively. Truly infinite would have to use a big decimal style library that can store arbitrarily large values.
      – David Parks
      Nov 12 at 18:50












    • No solution I see here proposes anything that is capable of going past sys.maxint, so I contend that this is still the cleanest and clearest way to code this, under the assumption that one doesn't need more than 9,223,372,036,854,775,807 loop iterations.
      – David Parks
      Nov 12 at 19:00






    • 1




      You're right it's a generator that is good enough in almost all practical cases, it's good way to cope with the practical problem. However it's limited to 2,147,483,647 on 32-bit machines, 2 giga is a reasonably achievable number in many applications. And it doesn't answer the question which is to make an infinite generator. Appart from that, you might not know that in python integers higher than sys.maxint are supported natively and transparently via the long type, for example try this in the python repl: print('%r, %r' % (sys.maxint, sys.maxint+1)) and notice the L after the number.
      – Eric
      Nov 13 at 19:20












    • Well I did learn something new then. I did not realize that python natively handled larger than long values. Nice.
      – David Parks
      Nov 13 at 21:42











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11424808%2feasy-way-to-keep-counting-up-infinitely%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    20
    down vote



    accepted










    Take a look at itertools.count().



    From the docs:




    count(start=0, step=1) --> count object



    Make an iterator that returns evenly spaced values starting with n.
    Equivalent to:




    def count(start=0, step=1):
    # count(10) --> 10 11 12 13 14 ...
    # count(2.5, 0.5) -> 2.5 3.0 3.5 ...
    n = start
    while True:
    yield n
    n += step


    So for example:



    import itertools

    for i in itertools.count(13):
    print(i)


    would generate an infinite sequence starting with 13, in steps of +1. And, I hadn't tried this before, but you can count down too of course:



    for i in itertools.count(100, -5):
    print(i)


    starts at 100, and keeps subtracting 5 for each new value ....









    share|improve this answer



















    • 1




      Beat me to it...
      – jamylak
      Jul 11 '12 at 2:45






    • 1




      @jamylak got lucky:)
      – Levon
      Jul 11 '12 at 2:45










    • I just couldn't stand the SO auto code formatter incorrect colors :)
      – jamylak
      Jul 11 '12 at 3:38















    up vote
    20
    down vote



    accepted










    Take a look at itertools.count().



    From the docs:




    count(start=0, step=1) --> count object



    Make an iterator that returns evenly spaced values starting with n.
    Equivalent to:




    def count(start=0, step=1):
    # count(10) --> 10 11 12 13 14 ...
    # count(2.5, 0.5) -> 2.5 3.0 3.5 ...
    n = start
    while True:
    yield n
    n += step


    So for example:



    import itertools

    for i in itertools.count(13):
    print(i)


    would generate an infinite sequence starting with 13, in steps of +1. And, I hadn't tried this before, but you can count down too of course:



    for i in itertools.count(100, -5):
    print(i)


    starts at 100, and keeps subtracting 5 for each new value ....









    share|improve this answer



















    • 1




      Beat me to it...
      – jamylak
      Jul 11 '12 at 2:45






    • 1




      @jamylak got lucky:)
      – Levon
      Jul 11 '12 at 2:45










    • I just couldn't stand the SO auto code formatter incorrect colors :)
      – jamylak
      Jul 11 '12 at 3:38













    up vote
    20
    down vote



    accepted







    up vote
    20
    down vote



    accepted






    Take a look at itertools.count().



    From the docs:




    count(start=0, step=1) --> count object



    Make an iterator that returns evenly spaced values starting with n.
    Equivalent to:




    def count(start=0, step=1):
    # count(10) --> 10 11 12 13 14 ...
    # count(2.5, 0.5) -> 2.5 3.0 3.5 ...
    n = start
    while True:
    yield n
    n += step


    So for example:



    import itertools

    for i in itertools.count(13):
    print(i)


    would generate an infinite sequence starting with 13, in steps of +1. And, I hadn't tried this before, but you can count down too of course:



    for i in itertools.count(100, -5):
    print(i)


    starts at 100, and keeps subtracting 5 for each new value ....









    share|improve this answer














    Take a look at itertools.count().



    From the docs:




    count(start=0, step=1) --> count object



    Make an iterator that returns evenly spaced values starting with n.
    Equivalent to:




    def count(start=0, step=1):
    # count(10) --> 10 11 12 13 14 ...
    # count(2.5, 0.5) -> 2.5 3.0 3.5 ...
    n = start
    while True:
    yield n
    n += step


    So for example:



    import itertools

    for i in itertools.count(13):
    print(i)


    would generate an infinite sequence starting with 13, in steps of +1. And, I hadn't tried this before, but you can count down too of course:



    for i in itertools.count(100, -5):
    print(i)


    starts at 100, and keeps subtracting 5 for each new value ....










    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 11 '12 at 3:32









    jamylak

    81k17173195




    81k17173195










    answered Jul 11 '12 at 2:45









    Levon

    82.7k22158161




    82.7k22158161








    • 1




      Beat me to it...
      – jamylak
      Jul 11 '12 at 2:45






    • 1




      @jamylak got lucky:)
      – Levon
      Jul 11 '12 at 2:45










    • I just couldn't stand the SO auto code formatter incorrect colors :)
      – jamylak
      Jul 11 '12 at 3:38














    • 1




      Beat me to it...
      – jamylak
      Jul 11 '12 at 2:45






    • 1




      @jamylak got lucky:)
      – Levon
      Jul 11 '12 at 2:45










    • I just couldn't stand the SO auto code formatter incorrect colors :)
      – jamylak
      Jul 11 '12 at 3:38








    1




    1




    Beat me to it...
    – jamylak
    Jul 11 '12 at 2:45




    Beat me to it...
    – jamylak
    Jul 11 '12 at 2:45




    1




    1




    @jamylak got lucky:)
    – Levon
    Jul 11 '12 at 2:45




    @jamylak got lucky:)
    – Levon
    Jul 11 '12 at 2:45












    I just couldn't stand the SO auto code formatter incorrect colors :)
    – jamylak
    Jul 11 '12 at 3:38




    I just couldn't stand the SO auto code formatter incorrect colors :)
    – jamylak
    Jul 11 '12 at 3:38












    up vote
    0
    down vote













    This is a bit smaller code than what the other user provided!



    x = 1
    while True:
    x = x+1
    print x





    share|improve this answer























    • I like count but this is also valid and can come in handy if you need multiple counters. Would update to x += 1
      – CasualDemon
      Oct 5 '17 at 21:04















    up vote
    0
    down vote













    This is a bit smaller code than what the other user provided!



    x = 1
    while True:
    x = x+1
    print x





    share|improve this answer























    • I like count but this is also valid and can come in handy if you need multiple counters. Would update to x += 1
      – CasualDemon
      Oct 5 '17 at 21:04













    up vote
    0
    down vote










    up vote
    0
    down vote









    This is a bit smaller code than what the other user provided!



    x = 1
    while True:
    x = x+1
    print x





    share|improve this answer














    This is a bit smaller code than what the other user provided!



    x = 1
    while True:
    x = x+1
    print x






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 20 '15 at 3:40









    Nicholas

    6,569144164




    6,569144164










    answered Mar 20 '15 at 2:49









    paebak

    91




    91












    • I like count but this is also valid and can come in handy if you need multiple counters. Would update to x += 1
      – CasualDemon
      Oct 5 '17 at 21:04


















    • I like count but this is also valid and can come in handy if you need multiple counters. Would update to x += 1
      – CasualDemon
      Oct 5 '17 at 21:04
















    I like count but this is also valid and can come in handy if you need multiple counters. Would update to x += 1
    – CasualDemon
    Oct 5 '17 at 21:04




    I like count but this is also valid and can come in handy if you need multiple counters. Would update to x += 1
    – CasualDemon
    Oct 5 '17 at 21:04










    up vote
    0
    down vote













    A little shorter, using an iterator and no library:



    x = 0
    for x in iter(lambda: x+1, -1):
    print(x)


    But it requires a variable in the current scope.






    share|improve this answer

























      up vote
      0
      down vote













      A little shorter, using an iterator and no library:



      x = 0
      for x in iter(lambda: x+1, -1):
      print(x)


      But it requires a variable in the current scope.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        A little shorter, using an iterator and no library:



        x = 0
        for x in iter(lambda: x+1, -1):
        print(x)


        But it requires a variable in the current scope.






        share|improve this answer












        A little shorter, using an iterator and no library:



        x = 0
        for x in iter(lambda: x+1, -1):
        print(x)


        But it requires a variable in the current scope.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 23:40









        Eric

        445417




        445417






















            up vote
            -1
            down vote













            xrange will give you an iterable object that won't use memory as you iterate, a for loop is cleaner than a while loop + counter if you ask me.



            import sys
            for i in xrange(0,sys.maxint):
            print i





            share|improve this answer























            • Not really infinite because it stops at sys.maxint, after which the domain of the long integers start automagically.
              – Eric
              Nov 10 at 23:37










            • Not infinite, but "similar to i++ in other languages" though. And of course x=0, 1, 2... will overflow at the same point that this fails anyway, so the solution you have posed has the same limitation effectively. Truly infinite would have to use a big decimal style library that can store arbitrarily large values.
              – David Parks
              Nov 12 at 18:50












            • No solution I see here proposes anything that is capable of going past sys.maxint, so I contend that this is still the cleanest and clearest way to code this, under the assumption that one doesn't need more than 9,223,372,036,854,775,807 loop iterations.
              – David Parks
              Nov 12 at 19:00






            • 1




              You're right it's a generator that is good enough in almost all practical cases, it's good way to cope with the practical problem. However it's limited to 2,147,483,647 on 32-bit machines, 2 giga is a reasonably achievable number in many applications. And it doesn't answer the question which is to make an infinite generator. Appart from that, you might not know that in python integers higher than sys.maxint are supported natively and transparently via the long type, for example try this in the python repl: print('%r, %r' % (sys.maxint, sys.maxint+1)) and notice the L after the number.
              – Eric
              Nov 13 at 19:20












            • Well I did learn something new then. I did not realize that python natively handled larger than long values. Nice.
              – David Parks
              Nov 13 at 21:42















            up vote
            -1
            down vote













            xrange will give you an iterable object that won't use memory as you iterate, a for loop is cleaner than a while loop + counter if you ask me.



            import sys
            for i in xrange(0,sys.maxint):
            print i





            share|improve this answer























            • Not really infinite because it stops at sys.maxint, after which the domain of the long integers start automagically.
              – Eric
              Nov 10 at 23:37










            • Not infinite, but "similar to i++ in other languages" though. And of course x=0, 1, 2... will overflow at the same point that this fails anyway, so the solution you have posed has the same limitation effectively. Truly infinite would have to use a big decimal style library that can store arbitrarily large values.
              – David Parks
              Nov 12 at 18:50












            • No solution I see here proposes anything that is capable of going past sys.maxint, so I contend that this is still the cleanest and clearest way to code this, under the assumption that one doesn't need more than 9,223,372,036,854,775,807 loop iterations.
              – David Parks
              Nov 12 at 19:00






            • 1




              You're right it's a generator that is good enough in almost all practical cases, it's good way to cope with the practical problem. However it's limited to 2,147,483,647 on 32-bit machines, 2 giga is a reasonably achievable number in many applications. And it doesn't answer the question which is to make an infinite generator. Appart from that, you might not know that in python integers higher than sys.maxint are supported natively and transparently via the long type, for example try this in the python repl: print('%r, %r' % (sys.maxint, sys.maxint+1)) and notice the L after the number.
              – Eric
              Nov 13 at 19:20












            • Well I did learn something new then. I did not realize that python natively handled larger than long values. Nice.
              – David Parks
              Nov 13 at 21:42













            up vote
            -1
            down vote










            up vote
            -1
            down vote









            xrange will give you an iterable object that won't use memory as you iterate, a for loop is cleaner than a while loop + counter if you ask me.



            import sys
            for i in xrange(0,sys.maxint):
            print i





            share|improve this answer














            xrange will give you an iterable object that won't use memory as you iterate, a for loop is cleaner than a while loop + counter if you ask me.



            import sys
            for i in xrange(0,sys.maxint):
            print i






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 7 '17 at 17:13

























            answered Apr 6 '17 at 4:32









            David Parks

            13.1k31108206




            13.1k31108206












            • Not really infinite because it stops at sys.maxint, after which the domain of the long integers start automagically.
              – Eric
              Nov 10 at 23:37










            • Not infinite, but "similar to i++ in other languages" though. And of course x=0, 1, 2... will overflow at the same point that this fails anyway, so the solution you have posed has the same limitation effectively. Truly infinite would have to use a big decimal style library that can store arbitrarily large values.
              – David Parks
              Nov 12 at 18:50












            • No solution I see here proposes anything that is capable of going past sys.maxint, so I contend that this is still the cleanest and clearest way to code this, under the assumption that one doesn't need more than 9,223,372,036,854,775,807 loop iterations.
              – David Parks
              Nov 12 at 19:00






            • 1




              You're right it's a generator that is good enough in almost all practical cases, it's good way to cope with the practical problem. However it's limited to 2,147,483,647 on 32-bit machines, 2 giga is a reasonably achievable number in many applications. And it doesn't answer the question which is to make an infinite generator. Appart from that, you might not know that in python integers higher than sys.maxint are supported natively and transparently via the long type, for example try this in the python repl: print('%r, %r' % (sys.maxint, sys.maxint+1)) and notice the L after the number.
              – Eric
              Nov 13 at 19:20












            • Well I did learn something new then. I did not realize that python natively handled larger than long values. Nice.
              – David Parks
              Nov 13 at 21:42


















            • Not really infinite because it stops at sys.maxint, after which the domain of the long integers start automagically.
              – Eric
              Nov 10 at 23:37










            • Not infinite, but "similar to i++ in other languages" though. And of course x=0, 1, 2... will overflow at the same point that this fails anyway, so the solution you have posed has the same limitation effectively. Truly infinite would have to use a big decimal style library that can store arbitrarily large values.
              – David Parks
              Nov 12 at 18:50












            • No solution I see here proposes anything that is capable of going past sys.maxint, so I contend that this is still the cleanest and clearest way to code this, under the assumption that one doesn't need more than 9,223,372,036,854,775,807 loop iterations.
              – David Parks
              Nov 12 at 19:00






            • 1




              You're right it's a generator that is good enough in almost all practical cases, it's good way to cope with the practical problem. However it's limited to 2,147,483,647 on 32-bit machines, 2 giga is a reasonably achievable number in many applications. And it doesn't answer the question which is to make an infinite generator. Appart from that, you might not know that in python integers higher than sys.maxint are supported natively and transparently via the long type, for example try this in the python repl: print('%r, %r' % (sys.maxint, sys.maxint+1)) and notice the L after the number.
              – Eric
              Nov 13 at 19:20












            • Well I did learn something new then. I did not realize that python natively handled larger than long values. Nice.
              – David Parks
              Nov 13 at 21:42
















            Not really infinite because it stops at sys.maxint, after which the domain of the long integers start automagically.
            – Eric
            Nov 10 at 23:37




            Not really infinite because it stops at sys.maxint, after which the domain of the long integers start automagically.
            – Eric
            Nov 10 at 23:37












            Not infinite, but "similar to i++ in other languages" though. And of course x=0, 1, 2... will overflow at the same point that this fails anyway, so the solution you have posed has the same limitation effectively. Truly infinite would have to use a big decimal style library that can store arbitrarily large values.
            – David Parks
            Nov 12 at 18:50






            Not infinite, but "similar to i++ in other languages" though. And of course x=0, 1, 2... will overflow at the same point that this fails anyway, so the solution you have posed has the same limitation effectively. Truly infinite would have to use a big decimal style library that can store arbitrarily large values.
            – David Parks
            Nov 12 at 18:50














            No solution I see here proposes anything that is capable of going past sys.maxint, so I contend that this is still the cleanest and clearest way to code this, under the assumption that one doesn't need more than 9,223,372,036,854,775,807 loop iterations.
            – David Parks
            Nov 12 at 19:00




            No solution I see here proposes anything that is capable of going past sys.maxint, so I contend that this is still the cleanest and clearest way to code this, under the assumption that one doesn't need more than 9,223,372,036,854,775,807 loop iterations.
            – David Parks
            Nov 12 at 19:00




            1




            1




            You're right it's a generator that is good enough in almost all practical cases, it's good way to cope with the practical problem. However it's limited to 2,147,483,647 on 32-bit machines, 2 giga is a reasonably achievable number in many applications. And it doesn't answer the question which is to make an infinite generator. Appart from that, you might not know that in python integers higher than sys.maxint are supported natively and transparently via the long type, for example try this in the python repl: print('%r, %r' % (sys.maxint, sys.maxint+1)) and notice the L after the number.
            – Eric
            Nov 13 at 19:20






            You're right it's a generator that is good enough in almost all practical cases, it's good way to cope with the practical problem. However it's limited to 2,147,483,647 on 32-bit machines, 2 giga is a reasonably achievable number in many applications. And it doesn't answer the question which is to make an infinite generator. Appart from that, you might not know that in python integers higher than sys.maxint are supported natively and transparently via the long type, for example try this in the python repl: print('%r, %r' % (sys.maxint, sys.maxint+1)) and notice the L after the number.
            – Eric
            Nov 13 at 19:20














            Well I did learn something new then. I did not realize that python natively handled larger than long values. Nice.
            – David Parks
            Nov 13 at 21:42




            Well I did learn something new then. I did not realize that python natively handled larger than long values. Nice.
            – David Parks
            Nov 13 at 21:42


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11424808%2feasy-way-to-keep-counting-up-infinitely%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine