[Python-Dev] Sets are mappings?

Michael Chermside mcherm at mcherm.com
Wed Dec 21 16:00:32 CET 2005


Nick Coghlan writes:
> Sorry - I meant to indicate that I didn't think the base classes were
> necessary because the relevant checks already existed in a "does it behave
> like one" sense:
>
>    def is_container(x):
        [...]
>    def is_mapping(x):
        [...]
>    def is_sequence(x):
        [...]
>    def is_multiarray(x):
        [...]

That sounds much more reasonable to me, although I'd also mention
that it is unusual to need to test for the "protocol support" as
you describe. Instead, it usually suffices to just USE the darn
thing and handle failures in an except clause. This is MORE powerful
than the hierarchy you describe, because it winds up testing for
only the features actually needed rather than testing for adherence
to some abstract base class.

An example should make it easy to understand. It is perfectly
reasonable for a container to support __getitem__, but not support
__len__. Perhaps the container uses an algorithm to generate the
items and is effectively of infinite size. In your hierarchy, this
wouldn't even be a basecontainer (and thus, clearly not a
basesequence). But if all you want to do is to use __getitem__ then
it ought to work fine.

-- Michael Chermside



More information about the Python-Dev mailing list