开发者

python 3.1 - DictType not part of types module?

开发者 https://www.devze.com 2023-01-17 08:34 出处:网络
This is what I found in my install of Python 3.1 on Windows. Where can I find other types, specifically DictType and StringTypes?

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 or str are. ...

Typical use is for isinstance() or issubclass() 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消