Re: Python-Dev Digest, Vol 5, Issue 57
Guido said:
There is the remaining issue of what exactly the syntax would be. I propose to extend the from clause to allow one or more dots before the dotted name, and to make the dotted name optional if at least one leading dot is found. I propose not to change from-less import.
What's the motivation for restricting it to from-imports? I suspect that this will cause confusion, and it doesn't feel self-consistent to me: why can you call a module ".foo" in one context, and not in another? In particular, I think that if people see that you can do: from .foo import * Then they'll assume that they can also do: import .foo With the obvious semantics, i.e. equivalence to: from . import foo What do we gain by not extending the syntax to from-less imports? -Edward
Hi,
"Edward Loper"
Guido said:
[...] I propose not to change from-less import.
What's the motivation for restricting it to from-imports? I suspect that this will cause confusion, and it doesn't feel self-consistent to me: why can you call a module ".foo" in one context, and not in another? In particular, I think that if people see that you can do:
from .foo import *
Then they'll assume that they can also do:
import .foo
The problem is probably, to what name should the module be bound? Variable names are not allowed to contain a leading dot. But it would be possible to say: import .foo as my_foo import foo and access both through modules through the name they're bound to. Therefore I'd propose to allow relative from-less imports, but require them to always use the "as" clause. regards Werner
[Ed]
What's the motivation for restricting it to from-imports? I suspect that this will cause confusion, and it doesn't feel self-consistent to me: why can you call a module ".foo" in one context, and not in another? In particular, I think that if people see that you can do:
from .foo import *
Then they'll assume that they can also do:
import .foo
With the obvious semantics, i.e. equivalence to:
from . import foo
What do we gain by not extending the syntax to from-less imports?
Currently, the rule is that after import <something> you can use <something> in an expression to refer to whatever you imported. This applies to both import foo <--> foo in expressions and import foo.bar <--> foo.bar in expressions But if we were to allow import .foo we'd have to allow .foo in expressions, and I would like to reserve that syntax for the "with" statement. [Werner]
But it would be possible to say:
import .foo as my_foo import foo
and access both through modules through the name they're bound to.
Therefore I'd propose to allow relative from-less imports, but require them to always use the "as" clause.
This just adds more complexity; there's nothing you can do without this addition. [Jack]
Is "from . import *" allowed? Whenever I start to think about it I tend to go into oscillation (yes because within package foo its the same as "from foo import *" on the outside; no because "import *" isn't allowed today either).
Yes, "from . import <whatever>" has the exact same meaning as spelling out the corresponding absolute module path after "from". --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (3)
-
Edward Loper
-
Guido van Rossum
-
Werner Schiendl