[Python-ideas] User-defined literals

Chris Angelico rosuav at gmail.com
Thu Jun 4 23:23:41 CEST 2015


On Fri, Jun 5, 2015 at 6:18 AM, Andrew Barnert via Python-ideas
<python-ideas at python.org> wrote:
> The best I can find is the rationale section for PEP 3119 saying "there are
> good reasons to keep the built-in types immutable", which is why PEP 3141
> was changed to not require mutating the built-in types. But "there are good
> reasons to allow implementations to forbid it" isn't the same thing as "all
> implementations must forbid it".
>
> And at least some implementations do allow it, like Brython and one of the
> two embedded pythons. (And the rationale in PEP 3119 doesn't apply to
> them--Brython doesn't share built-in types between different Python
> interpreters in different browser windows, even if they're in the same
> address space.)

Huh. Does that imply that Brython has to construct a brand-new integer
object for absolutely every operation and constant, in case someone
monkeypatched something? Once integers (and other built-in types) lose
their immutability, they become distinguishable:

x = 2
monkey_patch(x)
y = 2

In CPython (and, I think, in the Python spec), the two 2s in x and y
will be utterly indistinguishable, like fermions. CPython goes further
and uses the exact same object for both 2s, *because it can*. Is there
something you can do inside monkey_patch() that will "mark" one of
those 2s such that it's somehow different (add an attribute, change a
dunder method, etc)? And does Brython guarantee that id(x)!=id(y)
because of that?

ChrisA


More information about the Python-ideas mailing list