[Python-Dev] Re: Sets: elt in dict, lst.include

Moshe Zadka moshez@zadka.site.co.il
2001年1月31日 08:30:07 +0200 (IST)


On 2001年1月30日 19:49:24 -0500, Guido van Rossum <guido@digicool.com> wrote:
> There are different ways to do interators.
>> Here is a very "tame" proposal (and definitely in the realm of 2.2),
> that doesn't require any coroutine-like tricks. Let's propose that
>> for var in expr:
> 	...do something with var...
>> will henceforth be translated into
>> __iter = iterator(expr)
> while __iter.more():
> var = __iter.next()
> ...do something with var...

I'm +1 on that...but Tim's "try to use that to write something that
will return the nodes of a binary tree" still haunts me.
Personally, though, I'd thin down the interface to
while 1:
	try:
		var = __iter.next()
	except NoMoreError:
		break # pseudo-break?
With the usual caveat that this is a lie as far as "else" is concerned
(IOW, pseudo-break gets into the else)
> Then a new built-in function iterator() is needed that creates an
> iterator object. It should try two things:
>> (1) If the object implements __iterator__() (or a C API equivalent),
> call that and be done; this way arbitrary iterators can be
> created.
 
> (2) If the object smells like a sequence (how to test???), use an
> iterator sort of like this:

Why not, "if the object doesn't have __iterator__, try this. If it 
won't work, we'll find out by the exception that will be thrown in
our face".
class Iterator:
	def __init__(self, seq):
		self.seq = seq
		self.index = 0
	def next(self):
		try:
			try:
				return self.seq[self.index] # <- smells like
			except IndexError:
				raise NoMoreError(self.index)
		finally:
			self.index += 1
> (I don't necessarily mean that all those instance variables should
> be publicly available.)

But what about your poor brother? <wink> Er....I mean, this would make
implementing "indexing" really about just getting the index from the
iterator.
> If the argument to iterator() is itself an iterator (how to test???),

No idea, and this looks problematic. I see your point -- but it's
still problematic.
-- 
Moshe Zadka <sig@zadka.site.co.il>
This is a signature anti-virus. 
Please stop the spread of signature viruses!
Fingerprint: 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6

AltStyle によって変換されたページ (->オリジナル) /