This PEP proposes that symbols matching the type name should be addedto the types module for all basic Python types in the types module:
types.IntegerType->types.inttypes.FunctionType->types.functiontypes.TracebackType->types.traceback...
The long capitalized names currently in the types module will bedeprecated.
With this change the types module can serve as a replacement for thenew module. The new module shall be deprecated and listed inPEP 4.
A centralized repository of type names was a mistake. Neither the“types” nor “new” modules should be carried forward to Python 3.0.
In the meantime, it does not make sense to make the proposed updatesto the modules. This would cause disruption without any compensatingbenefit.
Instead, the problem that some internal types (frames, functions,etc.) don’t live anywhere outside those modules may be addressed byeither adding them to__builtin__ or sys. This will provide asmoother transition to Python 3.0.
Using two sets of names for the same objects is redundant andconfusing.
In Python versions prior to 2.2 the symbols matching many type nameswere taken by the factory functions for those types. Now all basictypes have been unified with their factory functions and therefore thetype names are available to be consistently used to refer to the typeobject.
Most types are accessible as either builtins or in the new module butsome types such as traceback and generator are only accessible throughthe types module under names which do not match the type name. ThisPEP provides a uniform way to access all basic types under a singleset of names.
The types module shall pass the following test:
importtypesfortinvars(types).values():iftype(t)istype:assertgetattr(types,t.__name__)ist
The types ‘class’, ‘instance method’ and ‘dict-proxy’ have alreadybeen renamed to the valid Python identifiers ‘classobj’,‘instancemethod’ and ‘dictproxy’, making this possible.
Because of their widespread use it is not planned to actually removethe long names from the types module in some future version. However,the long names should be changed in documentation and library sourcesto discourage their use in new code.
A reference implementation is available inissue #569328.
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-0294.rst
Last modified:2025-02-01 08:55:40 GMT