[Python-ideas] Adding`Unpicklable` to the `collections` module

Dima Tisnek dimaqq at gmail.com
Tue Nov 30 19:50:22 CET 2010


On 30 November 2010 10:58, Masklinn <masklinn at masklinn.net> wrote:
[snip]
> For instance, as of Python 2.7 these blocks of code are roughly equivalent (the first two are in fact identical in effect):
>
>    if isinstance(obj, collections.Sized):
>        doSomethingWith(len(obj))
>
>    if hasattr(obj, '__len__'):
>        doSomethingWith(len(obj))
>
>    try:
>        doSomethingWith(len(obj))
>    except TypeError:
>        pass
>
> They all test for the same thing: that you can call len() on the object. They don't test anything about your type hierarchy or which interfaces you inherit, which would be the only thing doable in Java (barring abuses of reflection). `collections.Sized` is simply a name for "I can call len() on this object".

I do get this, it just seems a bit overboard to make programmers learn
that Sized == len, Iterator == next+__iter__, and by induction
Stringed == upper,lower,translate,foo, Integral == +,-,*,/ etc.

What's the point to introduce all these new terms for old concepts?

As far as Java connotaions go, it's the -able suffix that makes it
very Java-like, and the concept that a semantic "interface" needs to
be defined and oddly named to get something done.
Surely Python is much more relaxed, yet the naming currently used is
not the best.

What I'm concerned about is that insinstance(anobj, pickle.Compatible)
this doesn't look very Python-like, if not Pythonic.


In the end, I guess, what 'user' is interested in, is not whether a
particular object has attribute "len", but the semantics of this
attribute, that it is callable, it returns something int-like and it
represents some notion like number of elements in something
container-like.

Of course we all need something like that, moreover I'm sure one day
we'll get to the point where objects gain and loose abc's during their
lifetime.



More information about the Python-ideas mailing list