why ast.literal_eval('1+1') is wrong in python2.7 [duplicate] - python

This question already has answers here:
Why does ast.literal_eval('5 * 7') fail?
(2 answers)
Closed 5 years ago.
When I execute code ast.literal_eval('1+1') in python2.7, the result as follows:
And I try it in python3.6, it works correctly. So what's the reason?

Both the 2.7 and 3.6 docs say the following:
This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.
Addition is an operator, so this is documented not to work. The fact that it does work in Python 3.6 is surprising to me. Searching the bug tracker, this discrepancy is listed as Python bug #31778.

Related

Where to find python .difference() source code? [duplicate]

This question already has answers here:
How to read Python source code directly from IDE
(3 answers)
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
I am using macOS, conda python 3.7 with PyCharm CE IDE.
When clicking into the function, the function didn't show any source code.
Therefore, where can I find the .difference() code?
It's written in C to improve performance (pycharm doesn't have access to the cpython source code, so it can't jump to the definition), you find it here: https://github.com/python/cpython/blob/main/Objects/setobject.c#L1481
The main details of the algorithm are from line 1531 and reasonably easy to follow. It basically iterates the first set, checking if each item is in the other set, if it is, add it to a result set, then returns the result set.
The code you're looking for starts here.
First thing it does is check that the two parameters are the same length. Then it goes through the first and checks for elements that are not present in the second, building up the result as it goes. Finally, it returns said result.

How do I block python RuntimeWarning from printing to the terminal? [duplicate]

This question already has answers here:
How to disable Python warnings?
(12 answers)
Closed 8 years ago.
I have some python code that, at some point, takes the axis means of a reasonably sparse array with nans in. Every time I run the code it raises a RuntimeWarning.
I know what's causing the warning, it's intentional, and it doesn't affect the output. It is, however, quite irritating to be presented with the warning every time I run the program - so, is there a cheap and nasty way to prevent them from being printed to the terminal?
This may be useful to you, I think the issue has been pretty solved in this question: How to disable python warnings
All what you need is suppress it exactly as described in official documentation: https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings

What does the underscore mean in python [duplicate]

This question already has answers here:
Why does python use two underscores for certain things? [duplicate]
(7 answers)
Closed 9 years ago.
Hi everyone as it obvious from my question I am like a brand new to python.
I am so confused when I am reading the documentation on python or even here in the Stackoverflow forum...
Why do they write like that
from __future__ import division
What does the underscore around the Future word mean ?? And Are we supposed to use it like that with the underscore in the python interpreter ?
This is just one of tons of examples. Any help would be greatly appericated.
According to PEP 236 where this module was proposed, the double underscores make it a reserved name.
[5] This ensures that a future_statement run under a release prior to
the first one in which a given feature is known (but >= 2.1) will
raise a compile-time error rather than silently do a wrong thing.
If transported to a release prior to 2.1, a runtime error will be
raised because of the failure to import __future__ (no such module
existed in the standard distribution before the 2.1 release, and
the double underscores make it a reserved name).

Is string formatting with % still allowed in Python 3.x? [duplicate]

This question already has answers here:
Python's many ways of string formatting — are the older ones (going to be) deprecated?
(5 answers)
Closed 9 years ago.
I read here that The plan is to eventually make this ["".format()] the only API for string formatting, and to start deprecating the % operator in Python 3.1.
I tried the % syntax with Python 3.1, 3.2 and 3.3, and it's working. So is there still a plan to remove the % syntax from Python in a future version, or can I use it freely?
This is not a definitive answer, but it is too large to make the point in a comment. The change in documentation wording in subsequent versions definitely moves away from stating the % syntax is deprecated.
From Old String Formatting Operations in v3.0 and Old String Formatting Operations in v3.1:
The formatting operations described here are obsolete and may go away in future versions of Python.
From Old String Formatting Operations in v3.2:
However, there are no current plans to deprecate printf-style formatting.
Old String Formatting Operations in v3.3 makes no mention of deprecation plans.
This is not quite certain enough for me to consider it actionable, however, and it would be nice to find a source with a clear statement.

Python 3 memory profiler or simple alternative [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there any working memory profiler for Python3
I have some script where I'd like to find out which objects are using up the memory. Moreover it should be for Python 3. I've found some modules suggested for that, but they are often old and not coded for Python 3. Also trying to install all of them to try out is quite a hassle.
Do you know which memory profiler could give me the following statistics or maybe you know a manual Python hack I can program adhoc to examine the rather short script (not written by me and hence a mess :()?
I'd like to see the object count after the run and moreover in which line this object was created. That's all :)
Any ideas?
EDIT:
One idea is to wrap all object instantiations by a class that counts the initialization in a class variable. It just would require to rewrite all instantiations of dicts, lists, etc.
I'm using objgraph which works with python3 without modifications.

Categories