On Mar 5, 2020, at 02:11, Steve Barnes <GadgetSteve@live.co.uk> wrote:
Comments in-line (I wish Outlook would behave sensibly)
-----Original Message----- From: Paul Moore <p.f.moore@gmail.com> Sent: 05 March 2020 09:52 To: Steve Barnes <GadgetSteve@live.co.uk> Cc: python-ideas@python.org Subject: Re: [Python-ideas] IDEA: Allow override of all assignments of a built in type to be a different type
On Thu, 5 Mar 2020 at 09:28, Steve Barnes <GadgetSteve@live.co.uk> wrote: Wouldn’t it be possible to have something along the lines of:
``` from decimal import TreatFloatsAsDecimal @TreatFloatsAsDecimal a = 0.1 # These are all now decimals b = 0.2 c = 0.3
a + b == c # This now works ```
I'm not at all clear how you imagine this would work. How would the scope of that TreatFloatsAsDecimal "decorator" be determined? How would the Decimal constructor get access to the original string representation of 0.1, rather than the float value? [Steve Barnes] To my mind a decorator would have function scope and there might be a file scope "magic". The interpreter would have to handle passing the raw value, (always a string at that stage), to the constructor rather than to the float constructor.
But it’s not a string at that stage. The compiler compiles float literals into float constant values attached to the code object. You need to change it to either put a Decimal value in the constants, or put the string in the constants and emit a call to Decimal in the bytecode. Either way, there’s nothing left for the interpreter to do; you’ve done it all at compile time.