Hi,
I just discovered the following pitfall in Python 2.3.
Consider the following code : a = {} bool (a.keys ())
False bool (a.iterkeys ())
True
So, an "empty" iterator evaluates to True in boolean context.
At a first glance, this is not what one would expect. This causes
several problems, e.g. if you operate on something expected to be
sequence, and you guard your code with "if seq :" to avoid crashing into
an empty sequence, you still crash if you get an empty iterator, even if
the rest of your code is able to deal with an iterator as well as with
any other sequence type.
At a second glance, the behaviour is clear, since the iterator object
does not know wheter it is able to provide the next element, before
having done so.
Anyway, although I don't know how the iterator protocol is implemented
in Python 2.3, I suppose the __nonzero__ could be changed to check
whether a next element can be provided without actually consuming it.
I would like to read some comments on this topic as I'm sure that I'm
not the first one wondering whether the current behaviour should be that
way.
thanks
chris 4 1584
Christian Eder wrote: Hi,
I just discovered the following pitfall in Python 2.3. Consider the following code :
>>> a = {} >>> bool (a.keys ()) False >>> bool (a.iterkeys ())
True
So, an "empty" iterator evaluates to True in boolean context. At a first glance, this is not what one would expect. This causes several problems, e.g. if you operate on something expected to be sequence, and you guard your code with "if seq :" to avoid crashing into an empty sequence, you still crash if you get an empty iterator, even if the rest of your code is able to deal with an iterator as well as with any other sequence type.
At a second glance, the behaviour is clear, since the iterator object does not know wheter it is able to provide the next element, before having done so.
Anyway, although I don't know how the iterator protocol is implemented in Python 2.3, I suppose the __nonzero__ could be changed to check whether a next element can be provided without actually consuming it.
I would like to read some comments on this topic as I'm sure that I'm not the first one wondering whether the current behaviour should be that way.
thanks chris
In fact, what is missing is a 'hasNext' function...
But for your example, the behavior is quite clear.
bool(a.keys()) == bool([]) == False
Whereas:
bool(a.iterkeys()) = bool(anIterator) == (anIterator != None) == True
So what is really missing is a hasNext method on Iterator. It would also
be usefull in some loop.
But what to do with Generator ?
Yermat
"Christian Eder" <ed**@tttech.com> wrote in message
news:c4**********@ttt14.vie.at.tttech.ttt... Hi,
I just discovered the following pitfall in Python 2.3. Consider the following code :
>>> a = {} >>> bool (a.keys ()) False >>> bool (a.iterkeys ()) True
So, an "empty" iterator evaluates to True in boolean context. At a first glance, this is not what one would expect. This causes several problems, e.g. if you operate on something expected to be sequence, and you guard your code with "if seq :" to avoid crashing into an empty sequence, you still crash if you get an empty iterator, even if the rest of your code is able to deal with an iterator as well as with any other sequence type.
At a second glance, the behaviour is clear, since the iterator object does not know wheter it is able to provide the next element, before having done so.
Anyway, although I don't know how the iterator protocol is implemented in Python 2.3, I suppose the __nonzero__ could be changed to check whether a next element can be provided without actually consuming it.
I would like to read some comments on this topic as I'm sure that I'm not the first one wondering whether the current behaviour should be that way.
One of the issues is that the iterator many not be able, even in
principle, be able to figure out whether there is, in fact, a next
element. Consider a pipe or an internet connection.
However, you're talking about the special iterator that the
dictionary object returns. I'm not sure whether this behavior
is a bug or a feature. It would clearly be possible for that
iterator object to proxy the __nonzero__ call to the basic
dictionary object, and from what you show, it doesn't do so.
I belive this is a quite different issue from a "has next" type
of function.
John Roth thanks chris
Christian Eder <ed**@tttech.com> wrote in message news:<c4**********@ttt14.vie.at.tttech.ttt>... <snip> I would like to read some comments on this topic as I'm sure that I'm not the first one wondering whether the current behaviour should be that way.
Indeed. You may want to read this thread of last summer: http://groups.google.it/groups?hl=it....lang.python.*
Michele Simionato
[Christian Eder] I just discovered the following pitfall in Python 2.3. Consider the following code :
>>> a = {} >>> bool (a.keys ()) False >>> bool (a.iterkeys ()) True
So, an "empty" iterator evaluates to True in boolean context. At a first glance, this is not what one would expect. This causes several problems, e.g. if you operate on something expected to be sequence, and you guard your code with "if seq :" to avoid crashing into an empty sequence, you still crash if you get an empty iterator, even if the rest of your code is able to deal with an iterator as well as with any other sequence type.
This was fixed in Py2.4:
Python 2.4a0 (#46, Apr 4 2004, 05:21:08) [MSC v.1200 32 bit (Intel)]
on win32
IDLE 1.1a0 d = dict(a=1, b=2, c=3) it = d.iterkeys() bool(it)
True list(it)
['a', 'c', 'b'] bool(it)
False
[John Roth] One of the issues is that the iterator many not be able, even in principle, be able to figure out whether there is, in fact, a next element. Consider a pipe or an internet connection.
However, you're talking about the special iterator that the dictionary object returns. I'm not sure whether this behavior is a bug or a feature. It would clearly be possible for that iterator object to proxy the __nonzero__ call to the basic dictionary object, and from what you show, it doesn't do so.
I belive this is a quite different issue from a "has next" type of function.
Right. So, the only iterators that provide knowledge of their length
are the ones like dictionaries that know what lies ahead. For the
rest, the behavior is unchanged (i.e. they do not provide a __len__()
method).
Raymond Hettinger This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven Bethard |
last post by:
So, as I understand it, in Python 3000, zip will basically be replaced
with izip, meaning that instead of returning a list, it will return an
iterator. This is great for situations like:
zip(*)...
|
by: deancoo |
last post by:
I have gotten into the habit of often using copy along with an insert
iterator. There are scenarios where I process quite a lot of data this way.
Can someone give me a general feel as to how much...
|
by: Marcin Kaliciñski |
last post by:
template<class RanAccIt>
void some_algorithm(RanAccIt begin, RanAccIt end)
{
// this algorithm involves calling std::lexicographical_compare
// on range [begin, end), and on reverse of this range...
|
by: codefixer |
last post by:
Hello,
I am trying to understand if ITERATORS are tied to CONTAINERS.
I know the difference between 5 different or 6(Trivial, on SGI).
But what I fail to understand is how can I declare all 5...
|
by: babak |
last post by:
Hi everyone
I have a problem with Iterators and containers in STL that hopefully
someone can help me with.
This is what I try to do:
I have an associative (map) container and I have a...
|
by: Lasse Vågsæther Karlsen |
last post by:
I need to merge several sources of values into one stream of values. All
of the sources are sorted already and I need to retrieve the values from
them all in sorted order.
In other words:
s1 = ...
|
by: Jiri Kripac |
last post by:
Languages such as Simula 67 contain a general concept of coroutines that
allow the execution of a method to be suspended without rolling back the
stack and then later resumed at the same place as...
|
by: ma740988 |
last post by:
typedef std::vector < std::complex < double > > complex_vec_type;
// option1
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( &cc, &cc );
}
versus
|
by: desktop |
last post by:
1)
I have this code:
std::list<intmylist;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_back(4);
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |