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?

> I do know that this goes against the explicit is better than implicit so an 
> alternative might be to have a flexible base modifier, something like:
>
> ```
> from decimal import DecimalBase as D
>
> a = 0D0.1  # These are all now decimals
> b = 0D0.2
> c = 0D0.3
>
> a + b == c # This now works
> ```

You can already do

```
from decimal import Decimal as D

a = D("0.1")  # These are all now decimals
b = D("0.2")
c = D("0.3")

a + b == c # This now works
```

so the main differences are (1) a few keystrokes, and (2) a conceptual
model closer to what you intend ("these are decimal numbers" rather
than "these are strings that we parse to get decimals"). I mention (2)
because it's often ignored, and yet it's probably the most important
reason people keep coming back to this.

But user-defined literals have been discussed many times in the past,
and never really got anywhere as an idea. I don't have links right
now, but a search for the mailing list archives should turn up some.
You should probably research those and explain how your proposal here
addresses the objections that have been raised previously. Personally,
I have a vague attachment to the idea in theory, but in practice I
don't think I'd ever use it (at least not for the use cases you
mention, and I can't think of any others that I would use it for) so
it would be a non-trivial increase in the complexity of the language
for little obvious benefit.

The other thing that often acts as a good argument for a proposal is
to point out some reasonably substantial bodies of existing,
real-world, code that would be improved by the proposal. This is again
where this idea often falls down - I don't actually know of any
substantial code base that even uses Decimal or Fraction types, much
less uses literals sufficiently frequently that a dedicated syntax
would help. (I'm not saying that Decimal and/or Fraction are useless -
I use them myself, but mostly in the REPL, or in adhoc code
experiments, not in actual applications).

Paul
_______________________________________________
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/RGOXUGXLMREU23PZZO2CO6WLZHV6KHIF/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to