I always wondered why the syntax for importing specific objects from a module is from module import x, y, z
instead of import x, y, z from module
.
It depends on the programming language syntax you use. It is easier for me to read a such import.
İs it easier to read and understand
From Grocery buy apple and orange
or
Buy apple and orange from grocery.
Buy apple and orange from supermarket
First one suits me better...
From https://docs.python.org/3/reference/simple_stmts.html#import:
import foo # foo imported and bound locally
import foo.bar.baz # foo.bar.baz imported, foo bound locally
import foo.bar.baz as fbb # foo.bar.baz imported and bound as fbb
from foo.bar import baz # foo.bar.baz imported and bound as baz
from foo import attr # foo imported and foo.attr bound as attr
I conclude it's a matter of local availability/binding.
Apart from asking Guido directly, I don't think your going to find any explanation of this.
The syntax has been around from the very beginning. The earliest version of python sources I could find was python 1.0.1. Looking at the changelog in the Grammar file we find references to even earlier versions. In version 2 of Python (I think we're talking 2nd release after 0.9.0) we have this note:
# added 'from' NAME option on import clause, and '*' to import all;
This was added at the same time as
# added class definition.
So the import statement sprang forth at the same time as classes were added to Python. This comes from when Python was Guido van Rossum's solo project. In other words, the answer you are looking for is lost in the sands of time.
Now, here's my speculation why the import statement reads from x import y
rather than import y from x
.
The documentation for the import statement provides the basic algorithm for implementing import:
Import statements are executed in two steps: (1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the import statement occurs). The statement comes in two forms differing on whether it uses the from keyword. The first form (without from) repeats these steps for each identifier in the list. The form with from performs step (1) once, and then performs step (2) repeatedly.
In both versions of the import statement the first step of this algorithm are leftmost. I assume that this was the most obvious ordering for a language implementer, even though English might read more naturally if the order were reversed.
In fact, it's not that strange. Look at how we "import", "include" or "require" in other languages. We always specify the namespace first. include "inc/config.php" in PHP for example. So in a way, it keeps our usual way of including files or modules.
A very wild guess and probably totally non-sense, but I knew that syntax from Modula-2 (man, that was twenty years ago, I feel old)... maybe Python was inspired by it ?
I don't know the complete heritage of this syntax, as it dates from Python 1.x days. But I find it useful to be able to scan down the left side of the source, and quickly find the module names that a script depends on. If a statement read "import a,b,c,d,e,really_long_name, alsdf,lsdf from blah", it would take me a while to find that this script depended on blah.