[Python-Dev] Allowing slicing of iterators

Nick Coghlan ncoghlan at iinet.net.au
Tue Jan 25 14:41:32 CET 2005


Batista, Facundo wrote:
> I think that breaking the common idiom...
> 
>   for e in something[:]:
>       something.remove(e)
> 
> is a no-no...

The patch doesn't change existing behaviour - anything which is already 
sliceable (e.g. lists) goes through the existing __getitem__ or __getslice__ 
code paths.

All the patch adds is two additional checks (the first for an iterator, the 
second for an iterable) before PyObject_GetItem fails with the traditional 
"TypeError: unsubscriptable object".

Defining __getitem__ also allows any given iterator or iterable type to override 
the default slicing behaviour if they so choose.

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


More information about the Python-Dev mailing list