[Python-3000] Type parameterization (was: Re: Type annotations: annotating generators)

Collin Winter collinw at gmail.com
Fri May 19 23:08:25 CEST 2006


On 5/19/06, Jim Jewett <jimjjewett at gmail.com> wrote:
> On 5/19/06, Collin Winter <collinw at gmail.com> wrote:
> > dict(normal=check3)["normal"] is not the same as dict["normal"] --
>
> Yes, they are.  They both call __getitem__.
>
> __getitem__ on a dictionary instance happens to be defined.
> __getitem__ on the dict class happens not to be defined.

dict(normal=check3)["normal"] invokes dict.__getitem__().
dict["normal"] invokes type.__getitem__().

That's why they're not the same.

> > Guido and I have been discussing the latter. When I said "redirected",
> > I meant that dict's metaclass would catch the __getitem__ call and
> > then invoke dict.__parameterize__() appropriately.
>
> I interpreted your suggestion as saying type annotations should be
> treated that way.
>
> Now it sounds like you are saying that a few specific classes (such as
> dict) should be given a custom metaclass, and start to behave this way
> in all situations, not just signature context.  User defined classes
> would need to explicitly request the same behavior.

I would imagine that __getitem__ would be defined on type, meaning
that all classes, user-defined and built-in, have this behaviour
automatically.

> I still think it has too many corner cases, both mentally and from an
> implementation standpoint.
>
>     class P:
>         def __parameterize__(self, T):
>
> Is P()[x] the same as P().__parameterize__(x)?  Then why not just call
> it __getitem__?

They are not the same: P()[x] would invoke P.__getitem__(), not
type.__getitem__().

>     class B:
>         def __getitem__(self, k): return 42
>         def __parameterize__(self, T): pass
>
> Since __getitem__ exists and does not delegate, is there any way that
> B.__parameterize__ will ever be called just by using brackets?  If so,
> then when?  If not, what good is it?

B.__parameterize__ will only be called in the case of B[x] (as opposed
to B()[x]).

Collin Winter


More information about the Python-3000 mailing list