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
. I'm not a native speaker, but isn't the latter more correct/natural?
So, what is the reason to put the from first? Is it merely to simplify the grammar (require less lookahead)? Is it an attempt to make the two kinds of imports visually more distinct? Or is it one of these cases where the obvious way is "not obvious at first unless you're Dutch"? ;)
No idea why it was actually done that way but it's the way I'd do it, simply because, being an engineering type, it seems more natural to me to start from a general category and drill down to specifics.
It would also mean the parser would have to store less stuff if processing sequentially. With:
import x, y, z from a
you have to remember x
, y
and z
. With:
from a import x, y, z
you only have to remember a
.
That's why I had so much trouble when I first encountered Perl's post-if variant:
$x = $y if $y > 40;
since you don't know in advance whether what you're reading is conditional or not.
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.
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.
It might make more sense in english to say import x, y, z from module
but in programming it makes much more sense to bring the more general Item first and then bring the details.
It might not be the reason but it makes things easier for the compiler or interpreter.
Try writing a compiler and you'll know what I mean :D
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...