list element of a class with the for statement

Tjabo Kloppenburg t.kloppenburg at billiton.de
Wed Mar 27 09:47:40 EST 2002


hi Just,

> But with the way you wrote the new-style iterator it won't. It's also
> not thread-safe.

Why wouldn't it work? I haven't tested the code, but I think it's the right 
way.
I've seen this in http://www.amk.ca/python/2.2/index.html, "3 PEP 234: 
Iterators".

>
Python classes can define an __iter__() method, which should create and 
return a new iterator for the object; if the object is its own iterator, this 
method can just return self. In particular, iterators will usually be their 
own iterators. Extension types implemented in C can implement a tp_iter 
function in order to return an iterator, and extension types that want to 
behave as iterators can define a tp_iternext function.

So, after all this, what do iterators actually do? They have one required 
method, next(), which takes no arguments and returns the next value. When 
there are no more values to be returned, calling next() should raise the 
StopIteration exception. 
>


How can I do it thread safe?


Willing to learn, :-)
tk.




More information about the Python-list mailing list