On Mar 5, 2020, at 02:11, Steve Barnes <gadgetst...@live.co.uk> wrote: > > Comments in-line (I wish Outlook would behave sensibly) > > -----Original Message----- > From: Paul Moore <p.f.mo...@gmail.com> > Sent: 05 March 2020 09:52 > To: Steve Barnes <gadgetst...@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 <gadgetst...@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. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/MW47EAM42S66YY57KJVC5L2THCFCJ35W/ Code of Conduct: http://python.org/psf/codeofconduct/