473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

itertools.count(-3)

itertools.count docs say:
Does not currently support python long integers.
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.

But it seems it doesn't support negative numbers too:
>>from itertools import count, islice
list(islice(count(), 0, 5))
[0, 1, 2, 3, 4]
>>list(islice(count(10), 0, 5))
[10, 11, 12, 13, 14]
>>list(islice(count(-3), 0, 5))
[4294967293L, 4294967294L, 4294967295L, 0, 1]
>>def count2(n=0):
.... while True:
.... yield n
.... n += 1
....
>>list(islice(count2(-3), 0, 5))
[-3, -2, -1, 0, 1]

If this isn't a bug, then maybe docs can tell about this too.

Bye,
bearophile

Sep 21 '06 #1
2 1750

bearophileH...@lycos.com wrote:
itertools.count docs say:
Does not currently support python long integers.
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.

But it seems it doesn't support negative numbers too:
>from itertools import count, islice
list(islice(count(), 0, 5))
[0, 1, 2, 3, 4]
>list(islice(count(10), 0, 5))
[10, 11, 12, 13, 14]
>list(islice(count(-3), 0, 5))
[4294967293L, 4294967294L, 4294967295L, 0, 1]
>def count2(n=0):
... while True:
... yield n
... n += 1
...
>list(islice(count2(-3), 0, 5))
[-3, -2, -1, 0, 1]

If this isn't a bug, then maybe docs can tell about this too.
Seems like a regression to me. itertools was documented as taking a
sequence of integers, not necessarily positive integers. It worked on
Python 2.4.

Looking at the source (from 2.5 rc2), it looks like they accidentally
used PyInt_FromSize_t rather than PyInt_FromSSize_t in the count
iterator. size_t is unsigned, of course, hence the large negative
numbers.
Carl Banks

Sep 21 '06 #2
On Thu, Sep 21, 2006 at 08:29:11AM -0700, Carl Banks wrote:
>
bearophileH...@lycos.com wrote:
itertools.count docs say:
Does not currently support python long integers.
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.

But it seems it doesn't support negative numbers too:
>>from itertools import count, islice
>>list(islice(count(), 0, 5))
[0, 1, 2, 3, 4]
>>list(islice(count(10), 0, 5))
[10, 11, 12, 13, 14]
>>list(islice(count(-3), 0, 5))
[4294967293L, 4294967294L, 4294967295L, 0, 1]
>>def count2(n=0):
... while True:
... yield n
... n += 1
...
>>list(islice(count2(-3), 0, 5))
[-3, -2, -1, 0, 1]

If this isn't a bug, then maybe docs can tell about this too.

Seems like a regression to me. itertools was documented as taking a
sequence of integers, not necessarily positive integers. It worked on
Python 2.4.

Looking at the source (from 2.5 rc2), it looks like they accidentally
used PyInt_FromSize_t rather than PyInt_FromSSize_t in the count
iterator. size_t is unsigned, of course, hence the large negative
numbers.
Nuts, that was me. I'll fix and add some tests.

-Jack
Sep 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Williams | last post by:
Is there any interest in adding a "Haskellish" take function to itertools? I know its easy enough to roll my own, I've been using: take = lambda iter, n: which works, but I was wondering if...
10
by: Jeremy Fincher | last post by:
Sometimes I find myself simply wanting the length of an iterator. For example, to collect some (somewhat useless ;)) statistics about a program of mine, I've got code like this: objs =...
6
by: Robert Brewer | last post by:
def warehouse(stock, factory=None): """warehouse(stock, factory=None) -> iavailable, iremainder. Iterate over stock, yielding each value. Once the 'stock' sequence is exhausted, the factory...
1
by: anton muhin | last post by:
Hello, everybody! Trying to solve the problem in the subj, I found that I miss some iterator-related tools. Mostly consequental application of the same function to some argument (if I'm not...
1
by: Steven Bethard | last post by:
Is there a reason that itertools.islice doesn't support None arguments for start and step? This would be handy for use with slice objects: >>> r = range(20) >>> s1 = slice(2, 10, 2) >>> s2 =...
18
by: Ville Vainio | last post by:
For quick-and-dirty stuff, it's often convenient to flatten a sequence (which perl does, surprise surprise, by default): ]]] -> One such implementation is at ...
21
by: Steven Bethard | last post by:
Jack Diederich wrote: > > itertools to iter transition, huh? I slipped that one in, I mentioned > it to Raymond at PyCon and he didn't flinch. It would be nice not to > have to sprinkle 'import...
41
by: rurpy | last post by:
The code below should be pretty self-explanatory. I want to read two files in parallel, so that I can print corresponding lines from each, side by side. itertools.izip() seems the obvious way to...
23
by: Mathias Panzenboeck | last post by:
I wrote a few functions which IMHO are missing in python(s itertools). You can download them here: http://sourceforge.net/project/showfiles.php?group_id=165721&package_id=212104 A short...
17
by: Raymond Hettinger | last post by:
I'm considering deprecating these two functions and would like some feedback from the community or from people who have a background in functional programming. * I'm concerned that use cases for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.