views:

50

answers:

2

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
CodeType
FrameType
FunctionType
GeneratorType
GetSetDescriptorType
LambdaType
MemberDescriptorType
MethodType
ModuleType
TracebackType
__builtins__
__doc__
__file__
__name__
__package__
>>> 
+4  A: 

According to the doc of the types module,

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.)

KennyTM
+1  A: 

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.

unutbu
@ubuntu Am actually dealing with a 2.x script conversion. isinstance(x,dict) works for now. Thanks.
hyper_w