[Python-3000] the types module
tomerfiliba at gmail.com
Thu Feb 1 12:43:03 CET 2007
i've had some difficulty with code that attempts to locate a type
by its __module__ and __name__, something like:
the trouble is, all builtin types claim to belong to the __builtin__ module.
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.
on the other hand, the exceptions module works differently. all builtin
exceptions are defined in the exceptions module, but are exposed
so i thought why not do the same with all builtin types? currently the
types module (types.py) exposes some type objects (not all), and uses
witchcraft to obtain them:
tb = sys.exc_info()
TracebackType = type(tb)
FrameType = type(tb.tb_frame)
instead, let's make it a builtin module, in which all types will be defined;
the useful types (int, str, ...) would be exposed into __builtin__ (just as
the exceptions module does), while the less useful will be kept unexposed.
this would make FunctionType.__module__ == "types", rather than
which would allow me to fetch it by name from sys.modules.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-3000