[Python-Dev] ABC issues
Guido van Rossum
guido at python.org
Tue May 27 21:39:45 CEST 2008
On Tue, May 27, 2008 at 12:16 PM, Armin Ronacher
<armin.ronacher at active-4.com> wrote:
> Hi,
>
> Guido van Rossum <guido <at> python.org> writes:
>
>> There's no need to register as Sized -- the Sized ABC recognizes
>> classes that define __len__ automatically. The Container class does
>> the same looking for __contains__. Since the deque class doesn't
>> implement __contains__, it is not considered a Container -- correctly
>> IMO.
> True. deque doesn't implement __contains__. However "in" still works
> because of the __iter__ fallback.
Sure, but that fallback is slow, and intentionally does not trigger
the 'Container' ABC.
> So from the API's perspective it's
> still compatible, even though it doesn't implement it. The same probably
> affects old style iterators (__getitem__ with index). One could argue that
> they are still iterable or containers, but that's harder to check so
> probably not worth the effort.
The ABCs do not intend to capture partial behavioral compatibility
that way -- the intent is to capture interface (in the duck typing
sense). Whether __iter__ is a good enough substitute for __contains__
depends on a lot of things -- surely for a huge list it isn't, and for
an iterator it isn't either (since it modifies the iterator's state).
>> >> Another issue is that builtin types don't accept ABCs currently. For
>> >> example
>> >> set() | SomeSet() gives a TypeError, SomeSet() | set() however works.
>> >
>> > Pandora's Box -- sure you want to open it?
>>
>> In 3.0 I'd like to; this was my original intent. In 2.6 I think it's
>> not worth the complexity, though I won't complain.
> I would love to help on that as I'm very interested in that feature.
Please do submit patches!
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list