[Python-Dev] __reversed__ protocol
Brett C.
bac at OCF.Berkeley.EDU
Tue Nov 18 17:07:27 EST 2003
Raymond Hettinger wrote:
> At one point, PEP 322 had proposed checking to see if an object defined
> __reversed__ and if not available, then proceeding normally using
> __getitem__ and __len__. While the idea had supporters, it got taken
> out because Guido worried that it would be abused by being applied to
> general iterables like generators and objects returned by itertools.
>
> So, an improved version of the idea is to check for __reversed__ but
> only use it when the object also defines __len__. That precludes the
> abuses but leaves the protocol open for the normal use cases. The
> simple patch is listed below.
>
> Guido doesn't have time for this now and asked me to present it to you
> guys. What do you guys think?
>
With 'reversed' now a built-in, it seems reasonable to have some magic
method support for it. Then again it does add one more thing to have to
be aware of that is not necessarily needed.
As for the solution in terms of the problem, I think it is a great way
to handle it. It should cause people to think more about supporting
__reversed__ then had they not had to define __len__.
So, with 'reversed' in the language, I am +0 on adding this with a
slight leaning toward +1 if I come across a personal need for 'reversed'
itself.
-Brett
More information about the Python-Dev
mailing list