[Python-3000] the types module

Brett Cannon brett at python.org
Thu Feb 1 20:12:20 CET 2007


On 2/1/07, tomer filiba <tomerfiliba at gmail.com> wrote:
> i've had some difficulty with code that attempts to locate a type
> by its __module__ and __name__, something like:
>     getattr(sys.modules[t.__module__], t.__name__)
>
> the trouble is, all builtin types claim to belong to the __builtin__ module.
> for example:
>     >>> types.FunctionType
>     <type 'function'>
>     >>> types.FunctionType.__name__
>     'funcrtion'
>      >>> types.FunctionType.__module__
>     '__builtin__'
>
> but --
>     >>> __builtin__.function
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     AttributeError: 'module' object has no attribute 'function'
>
> most, but not all, of the types are exposed in __builtin__... this required
> me to create an artificial mapping in which "__builtin__.function" is mapped
> to types.FunctionType, and then use this mapping instead of sys.modules,
> which adds more special cases on my part.
>

This has come up before on python-dev, IIRC.  Double-check the archives.

-Brett


More information about the Python-3000 mailing list