Ok – a bit more, and hopefully narrowing down the discussion here:
As far as I can tell, the OP hasn’t posed since the initial request. So who knows what they think now? But they did ask for int to accept e notation as a string, not as a literal, with the example of command line user input as the example use case.
But this caught my eye for two reasons:
-
I Do fairly often want to input large integers in “e-notation”[*] – honestly, they are usually converted to float anyway, but not always.
-
As it happens, right before this thread was started, I had proposed a polymorphic function for rounding to significant figures:
def sigfigs(x, n):
return type(x)(f"{x:.{n}g}")
[what the heck is discourse doing with that colorization?]
This works for float
and Decimal
(and will work for Fraction
in the next Python version) but not for int
, because the int
string parser does not accept e-notation. And it could work for any type that defines g
formation and can parse e-notation.
Is this an important use-case? maybe not, but that’s why this caught my attention. Also note that in this use case, having a separate flag or function wouldn’t help. Granted, there are only a few numeric types built in to Python, so it wouldn’t be a big deal to special case them all (or only int
) but still, I like the simplicity of that version. (I could also add another trip through Decimal to make it work for int, but again, not so simple)
So here’s a specific tiered proposal:
1st choice:
int()
parses an e-formatted string, and:
- if the result is an integer, creates an integer with that value
- if the result is not an integer, raises a ValueError
- if base > 14 (I think) – raises a ValueError (or base !=10)
- if it can’t be properly parsed, raises a ValueError (of course)
2nd choice:
If the above is too much overhead to do for every int
string parsing, OR it’s decided that it’s too backward incompatible, then:
- have a boolean flag,
sci
that activates all of the above.
Note on backward compatibility – I think it’s not a big deal, but as it is an idiom to do:
try:
value = int(input_string)
except ValueError:
print(f"input{input_string}: not valid, must be an integer")
Maybe we shouldn’t change the behavior of default string parsing. Personally, I’d love to see e-notation NOT get rejected by this code – why I’m supporting this in the first place, but others may not agree.
3rd choice:
One of the above two, but accept only:
[an_integer]e[a_posative_integer]
Frankly, I don’t see the need for this restriction, except maybe to make parsing easier – while it’s been said in this thread that e-notation is inherently for float – I think that’s only the perspective of programmers that think about type – which is not what the use cases presented so far are about. In fact, the g
and e
formatters don’t follow that convention, so my use case two would be dead if that restriction were in place anyway.
-CHB
[*] if you’re curious, for our oil spill model, oceanic turbulence generally has a diffusion coefficient of around 1^5 cm^2/s – so 1e5 is a lot easier to read and write than 100000 or 10_000. And the values are always of that magnitude, so no need for a float.