Allowing import star with namespaces

We all know that doing:
from pkg import *
is bad because it obliterates the 'pkg' namespace. So why not allow something like:
import pkg.*
This would still be helpful for interactive sessions while keeping namespaces around.
Sorry if this has been brought up before, my searching didn't find anything relevant in the archives.
Thanks, Brendan

Brendan Moloney wrote:
We all know that doing:
from pkg import *
is bad because it obliterates the 'pkg' namespace. So why not allow something like:
I don't quite know what you mean by obliterating the pkg namespace, but if my guess is correct, you're wrong. One of the problems with import * is that it (potentially) obliterates the caller's namespace, not pkg. That is, if you have a function spam(), and you do "from module import *", and module also has spam(), it blows away your function. The pkg namespace isn't touched -- it's just unavailable to the caller.
import pkg.*
This would still be helpful for interactive sessions while keeping namespaces around.
I don't understand what the difference between that and just "import pkg" would be.
By the way, this sort of question should probably go to the python-ideas mailing list for any extended discussion.

Brendan Moloney wrote:
We all know that doing:
--> from pkg import *
is bad because it obliterates the 'pkg' namespace.
The strongest reason for not doing this is that it pollutes the current namespace, not that it obliterates the 'pkg' namespace.
So why not allow something like:
--> import pkg.*
How would that be different from
--> import pkg
?
If you want convenience for interactive work, you can always:
--> import pkg --> from pkg import *
and then have the best (and worst!) of both techniques.
~Ethan~

Ethan Furman wrote:
The strongest reason for not doing this is that it pollutes the current namespace, not that it obliterates the 'pkg' namespace.
Sorry, I phrased that badly. When I said "obliterates the 'pkg' namespace" I was referring to dumping the 'pkg' namespace into the current namespace (polluting it, as you would say).
How would that be different from --> import pkg
Because that does not import all of the (public) modules and packages under 'pkg'. For example scipy has has a subpackage 'linalg'. If I just do 'import scipy' then I can not refer to 'scipy.linalg' until I do 'import scipy.linalg'.
Steven D'Aprano wrote:
By the way, this sort of question should probably go to the python-ideas mailing list for any extended discussion.
Sorry, didn't realize that would be the more appropriate list.
Thanks, Brendan
participants (3)
-
Brendan Moloney
-
Ethan Furman
-
Steven D'Aprano