python 3.1 - DictType not part of types module?
This is what I found in my install of Python 3.1 on Windows.
Where can I find other types, specifically DictType and StringTypes?
>>> print('\n'.join(dir(types)))
BuiltinFunctionType
BuiltinMethodType
CodeT开发者_开发技巧ype
FrameType
FunctionType
GeneratorType
GetSetDescriptorType
LambdaType
MemberDescriptorType
MethodType
ModuleType
TracebackType
__builtins__
__doc__
__file__
__name__
__package__
>>>
According to the doc of the types
module (http://docs.python.org/py3k/library/types.html),
This module defines names for some object types that are used by the standard Python interpreter, but not exposed as builtins like
int
orstr
are. ...Typical use is for
isinstance()
orissubclass()
checks.
Since the dictionary type can be used with dict
, there is no need to introduce such a type in this module.
>>> isinstance({}, dict)
True
>>> isinstance('', str)
True
>>> isinstance({}, str)
False
>>> isinstance('', dict)
False
(The examples on int
and str
are outdated too.)
Grepping /usr/lib/python3.1 for 'DictType' shows its only occurrence is in /usr/lib/python3.1/lib2to3/fixes/fix_types.py
. There, _TYPE_MAPPING
maps DictType
to dict
.
_TYPE_MAPPING = {
'BooleanType' : 'bool',
'BufferType' : 'memoryview',
'ClassType' : 'type',
'ComplexType' : 'complex',
'DictType': 'dict',
'DictionaryType' : 'dict',
'EllipsisType' : 'type(Ellipsis)',
#'FileType' : 'io.IOBase',
'FloatType': 'float',
'IntType': 'int',
'ListType': 'list',
'LongType': 'int',
'ObjectType' : 'object',
'NoneType': 'type(None)',
'NotImplementedType' : 'type(NotImplemented)',
'SliceType' : 'slice',
'StringType': 'bytes', # XXX ?
'StringTypes' : 'str', # XXX ?
'TupleType': 'tuple',
'TypeType' : 'type',
'UnicodeType': 'str',
'XRangeType' : 'range',
}
So I think in Python3 DictType
is replaced by dict
.
精彩评论