Method returning an Iterable Object

Anjanesh Lekshminarayanan mail at anjanesh.net
Mon Jan 26 13:35:53 EST 2009


> You can also replace the whole class with a function thusly:
>
>    def two_times(n):
>        for k in itertools.count(1):
>            yield n * (2**k)
>
> This function is then called a generator (because it generates an
> iterator). You can now say
>
>    infinitely_doubling_numbers = two_times(2)
>    for number in in infinitely_doubling_numbers:
>        print number

Oh..this is new. Will checkup itertools. Thanks.

>> t = twoTimes(5)
>> while (n in t.getNext()): # while (n in t):
>>     print (n)
>>
> You are aware that this is an infinite loop, as is my example above BTW?
> (Probably just an example, but I ask just in case.)

I was aware this was an infinite loop - just didnt want to put more
code for an example.



More information about the Python-list mailing list