[Python-ideas] add __contains__ into the "type" object

Jürgen A. Erhard jae+python at jaerhard.com
Wed Mar 1 22:23:08 EST 2017


On Tue, Feb 28, 2017 at 03:35:31PM -0800, Jelle Zijlstra wrote:
> 2017-02-28 15:12 GMT-08:00 Steven D'Aprano <steve at pearwood.info>:
> > On Wed, Mar 01, 2017 at 07:02:23AM +0800, 语言破碎处 wrote:
> >>
> >> where we use types?
> >>     almost:
> >>         isinstance(obj, T);
> >>         # issubclass(S, T);
> >>
> >> Note that TYPE is SET;
> >
> > What does that mean? I don't understand.
> >
> >
> >>     if we add __contains__ and __le__ into "type",
> >>     then things become:
> >>         obj in T;
> >
> > But obj is **not** in T, since T is a type, not a container.
> >
> 
> But in type theory, types are sets in some sense. For example, the
> bool type is the set {True, False}, and the int type is the infinite
> set {..., -1, 0, 1, ...}. Similarly, typing.py has a Union type:
> Union[A, B] is the union of the types A and B. Subclasses are subsets
> of their parent classes, because their set of possible values is a
> subset of the possible values of their parent class.
> 
> The OP seems to be proposing that we reflect this identity between
> types and sets in Python by spelling "isinstance(obj, T)" as "obj in
> T" and "issubclass(S, T)" as "S <= T". This proposal has some solid
> theory behind it and I don't think it would be hard to implement, but
> it doesn't seem like a particularly useful change to me. It wouldn't
> really enable anything we can't do now, and it may be confusing to
> people reading code that "obj in list" does something completely
> different from "obj in list()".

So?  Compare to "fn" vs "fn()" now.  Yes, some people are confused.
So what.  You *do* have to learn things.

And "enable anything we can't do now".  That argument was used any
number of times on this list, and even before this very list even
existed.  Still, we got decorators (they don't enable anything we
couldn't do without them, and we actually can still do what they do
without using them).

"isinstane" and "issubclass" are explicit!  Yay!... and decorators are
"implicit", and wouldn't you know it, they *do* confuse people.

I'm +.05 (and no, that's not because I finally see an idea of <sorry
can't type that name> that I actually think has some merit).  Maybe
even +1, given that isinstance(obj, class) is rather bulky.  Okay,
make that a +1.

Bye, J



More information about the Python-ideas mailing list