1

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.

amrx
  • 282
  • 4
  • 19
  • 1
    I'm guessing they used a special naming convention to avoid conflicts. Don't overthink it. You should use it exactly as it's written. – keyser Nov 28 '13 at 20:34
  • 1
    I'm going to answer this question with another answer: http://stackoverflow.com/a/3443428/1707253 – turnt Nov 28 '13 at 20:36
  • This has been answered before. http://stackoverflow.com/q/15090825/165103 – Mahmoud Hanafy Nov 28 '13 at 20:41
  • @MahmoudHossam: Instead of just adding that as a comment, click the "close" button and close it as a duplicate. – abarnert Nov 28 '13 at 20:44
  • @abarnert it's not an exact duplicate, the future module is a special case of __ module __, as explained in my answer. – Mahmoud Hanafy Nov 28 '13 at 20:52
  • @MahmoudHossam: `__future__` isn't a special case of `__module__`, it's a perfectly normal module, and also a special statement. But that doesn't matter; the OP clearly used `__future__` as an example here, and is asking about the double underscores in general. – abarnert Nov 28 '13 at 21:02
  • @abarnert Are all `__module__` reserved names? – Mahmoud Hanafy Nov 28 '13 at 21:12
  • @MahmoudHossam: I don't know what you mean by "reserved names". None of them are keywords. Any names with double underscores—whether module names, variable names, method names, etc.—are using the "special name" convention; some of them will be interpreted specially, some will not. It's recommended that you not use special names that aren't interpreted specially, because it's confusing to readers, and because future versions of Python might interpret them specially. I don't know whether all of that information together answers whatever question you had or not. – abarnert Nov 28 '13 at 21:26
  • @abarnert I think "reserved name" means that it's not currently taken by anything in CPython or other implementations. – Mahmoud Hanafy Nov 28 '13 at 21:29
  • Well, there are some special module names that are taken by something in Python and/or a Python implementation, like `__init__`. Any that aren't are, I guess, reserved in your terms. – abarnert Nov 28 '13 at 21:36
  • 1
    @MahmoudHossam: `__future__` is an ambiguous case—as a module name, it's nothing special, but the compiler processes a `from __future__ import foo` statement as a special `future` statement, _in addition to_ processing it as a normal `import` statement. The import machinery that does the second half doesn't treat the name specially at all, but the fact that it makes your statement also a `future` statement is certainly special in some sense. – abarnert Nov 28 '13 at 21:38

1 Answers1

3

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).
Mahmoud Hanafy
  • 7,958
  • 12
  • 47
  • 62