[prev in list] [next in list] [prev in thread] [next in thread]
List: python-ideas
Subject: [Python-ideas] Re: IDEA: Allow override of all assignments of a built in type to be a different type
From: Todd <toddrjen () gmail ! com>
Date: 2020-03-05 15:07:41
Message-ID: CAFpSVpLWnZWz9acHEcz-DRE3aW1XsU4WfHZZmH9j-vHOsumBxw () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
On Thu, Mar 5, 2020 at 4:27 AM Steve Barnes <GadgetSteve@live.co.uk> wrote:
> One of the lovely things about Python is that we have the capability to
> avoid issues such as the vagaries of the floating point type with libraries
> such as decimal and fractions. This is wonderous to me but comes with an
> issue that I suspect is limiting its usage. That issue is that you have to
> litter your code with constructors of those types, e.g.
>
>
>
> ```
>
> a = 0.1
>
> b = 0.2
>
> c = 0.3
>
> a + b == c # This doesn't work but it is easy to forget why
>
> ```
>
> vs:
>
> ```
>
> from decimal import * # I know why this is bad but it comes straight from
> the examples
>
> a = Decimal("0.1") # Needs to be a string to avoid being temporarily a
> float
>
> b = Decimal("0.2") # Ditto
>
> c = Decimal("0.3") # Ditto
>
> a + b == c # Magic this works!
>
> ```
>
>
>
> While this works it is not very friendly to users and there will often be
> cases where people forget to quote the values, etc. and there are similar
> issues with using the fractions library and I am sure with others. I
> suspect that this is why the decimals library, and some others, is not used
> as widely as it possibly should be.
>
>
>
> 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
>
> ``
>
>
If you were going to do this you would probably need something like a
context manager to control the scope, such as:
with TreatFloatsAsDecimal:
a = 0.1 # decimal
b = 0.2 # decimal
c = 0.3 # decimal
d = 0.3 # float
I have been interested in something like this in the context of numpy
arrays, but I am not sure even that would even be possible under how the
python language works, and if it was I figured it was too complicated to be
worthwhile.
[Attachment #5 (text/html)]
<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Mar \
5, 2020 at 4:27 AM Steve Barnes <<a \
href="mailto:GadgetSteve@live.co.uk">GadgetSteve@live.co.uk</a>> \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div lang="EN-GB">
<div class="gmail-m_-1562621738611829309WordSection1">
<p class="MsoNormal">One of the lovely things about Python is that we have the \
capability to avoid issues such as the vagaries of the floating point type with \
libraries such as decimal and fractions. This is wonderous to me but comes with an \
issue that I suspect is limiting its usage. That issue is that you have to litter \
your code with constructors of those types, e.g.<u></u><u></u></p> <p \
class="MsoNormal"><u></u> <u></u></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">```<u></u><u></u></span></p> <p \
class="MsoNormal"><span style="font-family:"Courier New"">a = \
0.1<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">b = 0.2<u></u><u></u></span></p> <p \
class="MsoNormal"><span style="font-family:"Courier New"">c = \
0.3<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">a + b == c # This doesn't work but it is \
easy to forget why<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">```<u></u><u></u></span></p> <p \
class="MsoNormal">vs:<u></u><u></u></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">```<u></u><u></u></span></p> <p \
class="MsoNormal"><span style="font-family:"Courier New"">from decimal \
import * # I know why this is bad but it comes straight from the \
examples<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">a = Decimal("0.1") # Needs to \
be a string to avoid being temporarily a float<u></u><u></u></span></p> <p \
class="MsoNormal"><span style="font-family:"Courier New"">b = \
Decimal("0.2") # Ditto<u></u><u></u></span></p> <p \
class="MsoNormal"><span style="font-family:"Courier New"">c = \
Decimal("0.3") # Ditto<u></u><u></u></span></p> <p \
class="MsoNormal"><span style="font-family:"Courier New"">a + b == c # \
Magic this works!<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">```<u></u><u></u></span></p> <p \
class="MsoNormal"><u></u> <u></u></p> <p class="MsoNormal">While this works it is \
not very friendly to users and there will often be cases where people forget to quote \
the values, etc. and there are similar issues with using the fractions library and I \
am sure with others. I suspect that this is why the decimals library, and some \
others, is not used as widely as it possibly should be.<u></u><u></u></p> <p \
class="MsoNormal"><u></u> <u></u></p> <p class="MsoNormal">Wouldn't it be possible \
to have something along the lines of:<u></u><u></u></p> <p class="MsoNormal"><u></u> \
<u></u></p> <p class="MsoNormal"><span style="font-family:"Courier \
New"">```<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">from decimal import \
TreatFloatsAsDecimal<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier \
New"">@TreatFloatsAsDecimal<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">a = 0.1 # These are all now \
decimals<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">b = 0.2<u></u><u></u></span></p> <p \
class="MsoNormal"><span style="font-family:"Courier New"">c = \
0.3<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier New"">a + b == c # This now \
works<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-family:"Courier \
New"">``</span></p></div></div><br></blockquote><div><br></div><div><br></div><div>If \
you were going to do this you would probably need something like a context manager to \
control the scope, such as:</div><div><br></div>with TreatFloatsAsDecimal:<br> a \
= 0.1 # decimal<br> b = 0.2 # decimal<br> c = 0.3 # decimal<br><br>d \
= 0.3 # float<div><br></div><div>I have been interested in something like this in \
the context of numpy arrays, but I am not sure even that would even be possible under \
how the python language works, and if it was I figured it was too complicated to be \
worthwhile. <br></div></div></div>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/H346THCDRRIGRIAQUVVU2PHRVH6T5VSP/
Code of Conduct: http://python.org/psf/codeofconduct/
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic