views:

127

answers:

7

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's the reason to put the from first? Is it merely to simplyfy the grammer (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"? ;)

+3  A: 

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 ?

DarkDust
Simplest answer, and most likely correct. GvR took constructs from various languages in designing Python, including Modula-2, and this is probably a direct lift from that language.
Paul McGuire
+2  A: 

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;

:-)

paxdiablo
Now that you mention "postfix if", I think I see a readability bonus... +1, will be accepted unless uber-answer bei A.M. comes along ;)
delnan
Oh, like A.M. needs more rep...
Paul McGuire
+1  A: 

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.

PJP
+2  A: 

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.

Paul McGuire
A: 

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

Scarlet
I fully realize the difference for the compiler writer, but if I *as a programmer* cared about that, I'd stick to assembly with mnemonics :D
delnan
Not only for the compiler writer, It also helps IDEs to give you option, if you where to first write 'import' the IDE couldn't give you you any option because it didn't know from what module you want to import
Scarlet
@Scarlet: Good point. It reminds me of the SQL statement `SELECT Columns FROM Table` which suffers from that very problem with auto-complete.
dan04
A: 

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

FallenAngel
+1  A: 

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.

Steven Rumbalski
Thanks for digging it out!
delnan