If the end goal is to just get either a float or an int from a string, and
you want to only accept floats the way Python spells them, what about using
ast.literal_eval?
>>> type(ast.literal_eval('1'))
>>> type(ast.literal_eval('1.0'))
>>> type(ast.literal_eval('01_105e-3'))
On Sun, Oct 3,
There are plenty of use cases if you pause to think. The other objections are
trivial. Even the simplest usage with the input() function is enough to warrant
its inclusion, considering that there are other useless string methods.
As I have had no support for the past few days, I quit the
On Sun, Oct 03, 2021 at 01:26:06AM -, Debashish Palit wrote:
> The floatify solution is a bit involved - using map inside a list
> comprehension. Usually list comprehensions replace the usage of map.
> Also floatify requires 5 extra lines of code (putting `return` on the
> same line is not
On Sat, Oct 02, 2021 at 01:06:46PM -, Debashish Palit wrote:
[...]
> Consider a number radix conversion software. It needs to accept the
> input number as a string (say '15A' in base 12). When we provide it a
> base 10 number, it would also be a string.
Why a string? That's an unusual API.
The floatify solution is a bit involved - using map inside a list
comprehension. Usually list comprehensions replace the usage of map. Also
floatify requires 5 extra lines of code (putting `return` on the same line is
not a best practice). Why not do it with one simple line of code ?
Debashish
On Sun, Oct 3, 2021 at 1:55 AM Debashish Palit wrote:
>
> Ok it is a 3 way flag, but the code is not awkward. 'c' is just a name I
> chose - it does not mean anything.
>
> The check for a valid number can be used on a list of strings -
>
> [float(s) for s in lst if s.isfloat() is not None]
>
>
Ok it is a 3 way flag, but the code is not awkward. 'c' is just a name I chose
- it does not mean anything.
The check for a valid number can be used on a list of strings -
[float(s) for s in lst if s.isfloat() is not None]
No 3 way flag here. Difficult to do with EAFP.
On Sat, Oct 02, 2021 at 01:21:46PM -, Debashish Palit wrote:
> Steven D'Aprano wrote:
> > On Sat, Oct 02, 2021 at 02:53:03AM -, Debashish Palit wrote:
> > > There is no need of a three_way_flag - just use a conditional
> > > expression instead of an if-elif-else block,
> Using a
> As I have mentioned above, EAFP is fine but you have to apply it twice.
> str.float accomplishes the same with 1 line of code.
> num = float(s) if s.float() else int(s)
There is a typo in my reply as quoted: it should be .isfloat() and not .float()
.
Steven D'Aprano wrote:
> On Sat, Oct 02, 2021 at 02:53:03AM -, Debashish Palit wrote:
> > There is no need of a three_way_flag - just use a conditional
> > expression instead of an if-elif-else block,
> > Of course you need a three way flag if your function returns a three way
> flag. It
Steven D'Aprano wrote:
> On Sat, Oct 02, 2021 at 02:17:32AM -, Debashish Palit wrote:
> > The method need not take care of every variation. Currently there is
> > no method to check for a float in a string even without formatting.
> > Right, because no method is needed. If you want to convert
On Sat, Oct 02, 2021 at 02:17:32AM -, Debashish Palit wrote:
> The method need not take care of every variation. Currently there is
> no method to check for a float in a string even without formatting.
Right, because no method is needed. If you want to convert something to
a float (whether
On Sat, Oct 02, 2021 at 02:53:03AM -, Debashish Palit wrote:
> There is no need of a three_way_flag - just use a conditional
> expression instead of an if-elif-else block,
Of course you need a three way flag if your function returns a three way
flag. It returns False for ints, True for
A correction to my reply: int('1234567890' * 100) will not raise an overflow
error, multiplying 1.0 with it raises the error. str.isfloat would still flag
the string as an int and then it is upto the application code to deal with the
overflow error.
Having such a method in the core helps avoid writing the function repeatedly.
(This would especially help beginners.)
My version is suited to the core language as a string method. parse_value as
given by Dennis Sweeney is suited to user code.
___
There is no need of a three_way_flag - just use a conditional expression
instead of an if-elif-else block,
str.isfloat uses the int() and float() functions, so, in your example, if float
returns inf we can still return True (or maybe return None in this case too).
If int() raises overflow
On Sat, Oct 2, 2021 at 12:19 PM Debashish Palit wrote:
>
> The method need not take care of every variation. Currently there is no
> method to check for a float in a string even without formatting. str.numeric
> or str.decimal don't work. str.isfloat would test unformatted strings for
> floats
The method need not take care of every variation. Currently there is no method
to check for a float in a string even without formatting. str.numeric or
str.decimal don't work. str.isfloat would test unformatted strings for floats
or ints just like the functions int() and float() convert strings
On Fri, Oct 01, 2021 at 08:56:39AM -, dpali...@outlook.com wrote:
> It would be nice to have a string method that checks for a float.
> Currently there is no support for this, either built-in or in the
> standard library. There is a thread, dating back to Dec 2020, that
> proposes a
I don't know how common it is to want to distinguish between string
representations of integers and those of floats. It seems like a function that
could have a million little variations: What if my boss says that now my input
integers can have commas? Or have extra space added? Or if
20 matches
Mail list logo