[Web-SIG] Iterator protocols.

Phillip J. Eby pje at telecommunity.com
Mon Aug 30 22:18:43 CEST 2004


At 09:02 PM 8/30/04 +0100, Alan Kennedy wrote:

>So the two consequent questions I have are
>
>1. Is there something wrong with my approach of defining a StopIteration 
>exception, and poking it into __builtin__?

Yes; it won't work with anything else that pokes its own StopIteration into 
__builtin__.  This is very fragile; don't do it.


>2. Do I need to implement the old pre-2.2 iterator protocol as well? It 
>had never occurred to me to implement that: I was focussed only on 2.2 
>iterators.

If you're writing a server or gateway, you don't need to implement it at 
all: use a "for" loop to iterate over the iterable, and all will be well.

If you're writing an application that must work under pre-2.2 Python, you 
must implement the *old* iterator protocol, and only that protocol.  You do 
not have to implement the new iterator protocol "as well".  Implement the 
old protocol *instead*.

Following these guidelines will make your code both "forward" and 
"backward" compatible, since newer Pythons still recognize the old iterator 
protocol.


>While we're on the subject of python 2.2 requisites, it's also trivial for 
>me to define True and False. Which leaves generators as the only 2.2 
>facility I can't do anything about. But since generators are optional for 
>application/middleware authors, doesn't that mean that 2.2.2 is not 
>required as the minimum version for framework authors, only for 
>2.2-dependent components that are plugged into their framework?

Correct.  By the way, there's no need to define True and False either; a 
server or gateway supporting a pre-2.2.2 version of Python should just use 
1 and 0.  The PEP doesn't actually require the use of True and False, it 
just refers to "true values" and "false values".



More information about the Web-SIG mailing list