[Python-Dev] buitlins instance have modifiable __class__?
Guido van Rossum
guido@python.org
Fri, 27 Sep 2002 20:17:50 -0400
> question on bultin types (under 2.2):
>
> >>> d={}
> >>> class ndict(dict):
> ... __slots__ = ()
> ... def __getitem__(self,k):
> ... print "__getitem__"
> ... return dict.__getitem__(self,k)
> ...
> >>> d.items()
> []
> >>> d['a']=3
> >>> d.__class__=ndict
>
> is intended to work?
>
> it seems it does, but is that the intention?
It was a mistake. In 2.3, it's disallowed. In 2.2.2, it'll still be
allowed, but you shouldn't do this -- all sorts of bizarre stuff can
happen because you can do this.
So if you're asking about this for Jython, please don't allow this in
Jython!
> typos apart, there was also another question, sorry I was typing and
> reflecting on the consequences of all of this on Jython ...
>
> [me]
> >
> > >>> exec "print a" in d
> > 3
> >
> > Ok, that is the non cooperative behavior I already know about. ]
> >
>
> I recall this was already discussed here, what is the idea, to leave
> it as it is or make this work?
That's not going to change in CPython, because I believe it would slow
down lookup for builtins and globals too much if we had to check for a
custom __getitem__. But if you can fix it for Jython, go ahead. I
don't mind if there are places where Jython is "purer" than CPython.
--Guido van Rossum (home page: http://www.python.org/~guido/)