Is there a good reason why when you try to take an element from an
already exausted iterator, it throws StopIteration instead of some other
exception ? I've lost quite some times already because I was using a lot
of iterators and I forgot that that specific function parameter was one.
Exemple : def f(i):
.... print list(i)
.... print list(i)
.... f(iter(range(2) ))
[0, 1]
[]
This is using Python 2.4.2
May 17 '06
21 1413 ya*****@gmail.c om wrote: Consider this example:
X = range(5) Y = iter(X) Z = iter(Y) As you can see, X is a container, and Y is an iterator. They are simliar in that "iter" works on them both.
Cristoph claims that this causes confusion. Why? Because "iter" doesn't have the same meaning for both of them. For X it always returns an iterator that yields the same set of values. For Y it returns an iterator yielding different values each time.
As an aside, perhaps iter(Y) should be returning a deeper
copy of Y. As it stands, I see 'Z=Y' giving the same effect: y=iter(x) z=iter(y) zip(y, z)
[(0, 1), (2, 3)] y=iter(x) z=y zip(y, z)
[(0, 1), (2, 3)]
[ ... ]
But changing "iter" to have the same meaning for containers and iterables is impossible. You cannot, conceptually, reiterate an iterator. So what Cristoph is suggesting - is to add an exception for the cases in which iterators and collections behave differently. Somewhat similar to this:
But the end, raising StopIteration is not where the
difference is. The difference is in iter(some_objec t).
iter(some_conta iner) creates a brand new iterator, standing
at the starting line and ready to run.
iter(some_listi terator), it seems, just returns a reference
to some_listiterat or, with some of its history possibly
behind it.
Iterators are used in the same syntactic places as general
iterables, but especially once people start giving them
names and handing them around, the real differences show up.
The short-range solution is Know What You're Doing. Duck
typing is a fine thing, but it works best for programmers
who keep scrupulous track of their objects' types.
Cheers, Mel.
In <44************ **********@news .free.fr>, Christophe wrote: Well, the main reason for such change is and will always be to catch bugs. The fact is, using duct typing is something very common with the Python language.
Duct typing? I guess you mean duct taping, or duck taping [1] or maybe
duck typing. :-)
SCNR,
Marc 'BlackJack' Rintsch
[1] http://www.cybersalt.org/cleanlaugh/...3/ducktape.htm This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ray Gardener |
last post by:
I was wondering if anyone had tried implementing fuzzy logic set
concepts in C++, because in FL, the concept of "type" or "class"
is fuzzy; things belong (or are of) a given type only by degree.
e.g., in a hypothetical fuzzy C++ language one could say:
class pickle : public vegetable 0.2
{
// pickle is not so much a vegetable as, say, onion is.
};
|
by: deancoo |
last post by:
If I have a container, say a vector, with 5 elements, and I initialize
iterator variables to point to the beginning and end of the container, are
those iterators going to always be valid if I replace the elements of the
container? The size stays the same. It seems to be working now, but I
thought I read somewhere something about this not always working out. Any
thoughts?
d
|
by: Dave Townsend |
last post by:
Hi,
Can anybody help me with the following piece of code?
The purpose behind the code is to parse HTML files, strip out
the tags and return the text between tags. This is part of a larger
application which will perform "searches" for text values in a
directory of html files, trying to match only the non-tagged text
in the documents.
|
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 =
s2 =
s3 =
for value in ???(s1, s2, s3):
|
by: Jack |
last post by:
I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.
I have another class which is a subclass of the "Base" class called
"Derived". This derived class has a member variable called
"m_derived". "m_derived" is initialized to "2" and is never changed.
I pass an array of "Base" classes...
| |
by: John Salerno |
last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part,
but it does in the second? Is the first statement something different?
False
print 'hi'
hi
Thanks.
|
by: Bo Yang |
last post by:
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
|
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: subramanian100in |
last post by:
Suppose I have
vector<intc;
for (int i = 0; i < 10; ++i)
c.push_back(i);
vector<int>::iterator it = find(c.begin(), c.end(), 5);
If I do,
c.insert(c.begin(), 10);
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |