What does the underscore mean in python [duplicate] - python

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).

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.

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

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.

In Python, given a class, how can I find what module it came from? [duplicate]

This question already has answers here:
Finding where something was imported from in python
(4 answers)
Closed 8 years ago.
When using the interactive shell in Spyder, for instance, all sorts of classes are in the global namespace, so it would be nice to be able to interactively find what module the class comes from.
The answer is buried in one of the responses to the hidden features of Python question:
You can ask any object which module it came from by looking at its
__module__ property. This is useful, for example, if you're
experimenting at the command line and have imported a lot of things.
Along the same lines, you can ask a module where it came from by
looking at its __file__ property. This is useful when debugging path
issues.

What are the access specifiers in python? [duplicate]

This question already has answers here:
Does Python have “private” variables in classes?
(15 answers)
Closed 9 years ago.
I have come from c++ and java background so, I was curious to know if python provides access specifiers as provided by c++/java. I've seen some code, and this is what I think,
__variable ---> private.
_variable ----> protected.
Correct me if I'm wrong.
Python has recommended practices rather than prescriptive ones - so anything with a _ at the start should be left alone by others but is not locked to prevent it. There is however name mangling to make life more interesting for members with a __ at the start - see PEP8.
Of course if others rely on your private data/methods rather then the public API they only have themselves to blame when you change something and their code stops working.
There is no such concept in Python. There are conventions that are used - like the ones Steve mentioned but also others such as calling the first variable of an instance method self.
In addition, for module level imports - there is a way to prevent the default behavior of importing all names from a module. This is done by populating __all__ with a list of names that should be imported (exposed) by default.
However, as with __var and _var it is just a convention (although one that is enforced by Python). It doesn't restrict you though - you can explicitly import any 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.

Categories