It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing. This can be used for evaluating strings containing Python values without the need to parse the values oneself. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None and Ellipsis. Since Python 2.6 you can use ast.literal_eval, and it's still available in Python 3.Įvaluate an expression node or a string containing only a Python literal or container display. Raise ValueError("invalid truth value %r" % (val,)) """Convert a string representation of truth to true (1) or false (0). Given that distutils will no longer be part of the standard library, here is the code for () (see the source code for 3.11.2). Raises ValueError if val is anything else.īe aware that () returns integer representations and thus it needs to be wrapped with bool() to get Boolean values. True values are y, yes, t, true, on and 1 false values are n, no, f, false, off and 0. Python >=3, =3.12: No longer part of the standard library due to PEP 632 – Deprecate distutils module.Warning: This answer will no longer work as of Python 3.12 (it's deprecated as of 3.10)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |