What is "<module '?' (built-in)>"?
Jonathan Hogg
jonathan at onegoodidea.com
Tue Jul 2 04:01:53 EDT 2002
On 2/7/2002 8:03, in article
45e6545c.0207012303.5a0e44fd at posting.google.com, "Seo Sanghyeon"
<unendliche at hanmail.net> wrote:
> Python 2.2.1 (#34, Apr 9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> IDLE 0.8 -- press F1 for help
> >>> from types import ModuleType as module
> >>> MyModule = module()
> >>> MyModule
> <module '?' (built-in)>
> >>> dir(MyModule)
> []
> >>>
>
> What is this?
You have constructed a new module dynamically. 'ModuleType' is the type of
module objects. Calling this instantiates a new module object. The new
module object is anonymous because you didn't give it a name, and "built-in"
because it didn't get loaded from anywhere. It's dir is empty because no
names have been set in the module's dictionary.
>>> MyModule.__name__ = 'MyModule'
>>> MyModule.__file__ = 'MyModule.py'
>>>
>>> MyModule
<module 'MyModule' from 'MyModule.py'>
>>>
>>> def hello(): print 'Hello World'
...
>>> MyModule.hello = hello
>>>
>>> dir(MyModule)
['__file__', '__name__', 'hello']
>>>
>>> MyModule.hello()
Hello World
>>>
This creates a module 'MyModule' exactly* as if you had written a file
called 'MyModule.py' containing the 'hello' function and then imported it.
Python lets you pretty much make anything dynamically that you like,
including modules and classes.
Jonathan
* OK, not quite exactly... ;-)
More information about the Python-list
mailing list