[Python-Dev] Very Strange Argument Handling Behavior
Steven D'Aprano
steve at pearwood.info
Sun Apr 18 00:21:57 CEST 2010
On Sun, 18 Apr 2010 07:47:08 am Maciej Fijalkowski wrote:
> On Sat, Apr 17, 2010 at 1:03 PM, Steve Holden <steve at holdenweb.com>
wrote:
> > Dino Viehland wrote:
> >> Maciej wrote:
> >
> > [...]
> >
> >>> And yet that breaks some code :-)
> >>
> >> Sure, if you do:
> >>
> >> class C(object):
> >> locals()[object()] = 42
> >>
> >> dir(C)
> >>
> >> You lose. Once I'm aware of some piece of code in the wild doing
> >> this then I'll be happy to change IronPython to be more
> >> compatible. :)
> >
> > This would be a lose anyway, since the CPython specifications
> > suggest that you should not rely on being able to change locals()
> > (or at least shouldn't expect that such changes are actually
> > reflected in the local namespace).
>
> You can override __new__ of a type subclass to achieve the same
> effect (or even directly call type.__new__ with strange dict as an
> argument).
I think that only works in Python 3.x, in 2.x the __dict__ is always a
regular dict no matter what you pass. At least for CPython. If there is
a way to set the dict of a class to something other than a regular dict
in CPython 2.x, I would be very pleased!
--
Steven D'Aprano
More information about the Python-Dev
mailing list