Where is my namespace?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Dec 7 17:17:16 EST 2009


On Mon, 07 Dec 2009 16:25:39 +0000, Simon Brunning wrote:

> 2009/12/7 vsoler <vicente.soler at gmail.com>:
[...]
> If you do "from blah import" the imported module itself isn't bound to
> any name in the importing module - you can't get at it at all.

Not quite -- you can get to it if you're willing to do some more work.

>>> from math import sin
>>> mod = __import__(sin.__module__)
>>> mod
<module 'math' from '/usr/local/lib/python3.0/lib-dynload/math.so'>


Alternatively, you can fetch it from sys.modules directly, but that's 
probably an implementation-specific trick.



>> 3. Mark says: The from statement is really an assignment to names in
>> the importer's scope--a name-copy operation, not a name aliasing.   I
>> don't fully understand what he means. Could anybody explain?

I'm not sure what Mark means by that either. It certainly isn't a copy 
operation, it doesn't duplicate the object you imported. I don't know 
what he means by aliasing, but if he means what I mean by aliasing, then 
I'd say the from statement *is* an aliasing operation: it creates a new 
name that refers to an existing object found by name.


from module import name

is roughly equivalent to:

import module
name = module.name
del module



-- 
Steven



More information about the Python-list mailing list