[Tutor] Python-list thread: int vs. float

boB Stepp robertvstepp at gmail.com
Sat Feb 11 15:28:42 EST 2017


On Fri, Feb 10, 2017 at 7:59 PM, boB Stepp <robertvstepp at gmail.com> wrote:
> I have been following the thread "int vs. float"
> (https://mail.python.org/pipermail/python-list/2017-February/719287.html)
> on the main list.  A search for the OP on the Tutor archive came up
> negative, so I am hoping he is not following Tutor tonight (Or anytime
> prior to the due date for his homework!).  The central part of
> adam14711993's question is:
>
> "What I cannot figure out is how to write it so that if my user input
> is, for example, 1.5, the program will result with: Sorry, you can
> only order whole packages.
>
> "I understand that because I am starting out by assigning my
> number_purchases_str to be an int, when the user enters a float that
> is a conflict and will crash."
>
> He cannot figure out how to reliably tell if the user's input is an
> integer, float or neither.

Back in the main Python list thread, Marko Rauhamaa suggested
(https://mail.python.org/pipermail/python-list/2017-February/719322.html):

"
...
Haven't been following the discussion, but this should be simply:

   ast.literal_eval("...")
...
"

This looks like it may do the trick quite concisely:

py3: import ast
py3: ast.literal_eval('5')
5
py3: ast.literal_eval('5.0')
5.0
py3: type(ast.literal_eval('5'))
<class 'int'>
py3: type(ast.literal_eval('5.0'))
<class 'float'>

It appears to reliably parse string input as to whether it is an
integer or float.  However, if the input is a non-numerical string:

py3: ast.literal_eval('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python35\lib\ast.py", line 84, in literal_eval
    return _convert(node_or_string)
  File "C:\Program Files\Python35\lib\ast.py", line 83, in _convert
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x0000000002C9D860>

But that seems okay, as in the OP's original problem spec, he just
needed a way to determine integer, float or other non-acceptable
input.  This seems to do this quite directly.  I am not familiar with
"abstract syntax trees" (Python has so much very interesting stuff!).
I am concerned about this paragraph at the beginning of the docs on it
(https://docs.python.org/3/library/ast.html?highlight=ast.literal_eval#module-ast):

"The ast module helps Python applications to process trees of the
Python abstract syntax grammar. The abstract syntax itself might
change with each Python release; this module helps to find out
programmatically what the current grammar looks like."

That statement about potentially changing with each Python release
bothers me.  Should I be bothered?  The link to ast.literal_eval():
https://docs.python.org/3/library/ast.html?highlight=ast.literal_eval#ast.literal_eval

What are the gotchas or other negatives from this approach?

boB


More information about the Tutor mailing list