2010-09-22 42 views
4

這是我在Windows上安裝Python 3.1時發現的。python 3.1 - DictType不是類型模塊的一部分?

我在哪裏可以找到其他類型,特別是DictType和StringTypes?

>>> print('\n'.join(dir(types))) 
BuiltinFunctionType 
BuiltinMethodType 
CodeType 
FrameType 
FunctionType 
GeneratorType 
GetSetDescriptorType 
LambdaType 
MemberDescriptorType 
MethodType 
ModuleType 
TracebackType 
__builtins__ 
__doc__ 
__file__ 
__name__ 
__package__ 
>>> 

回答

7

按照types模塊(http://docs.python.org/py3k/library/types.html)的文檔,

該模塊定義名稱用於由標準Python解釋中使用的一些對象類型,但不暴露像int或內建str是。 ...

典型用途是針對isinstance()issubclass()檢查。

由於字典類型可與dict一起使用,因此不需要在此模塊中引入此類型。

>>> isinstance({}, dict) 
True 
>>> isinstance('', str) 
True 
>>> isinstance({}, str) 
False 
>>> isinstance('', dict) 
False 

(的上intstr實例是過時的了。)

1

關於 'DictType' Grepping /usr/lib/python3.1顯示其僅發生在/usr/lib/python3.1/lib2to3/fixes/fix_types.py。在那裏,_TYPE_MAPPINGDictType映射到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', 
    } 

所以我覺得在Python3 DictTypedict取代。

+0

@ubuntu實際上是處理2.x腳本轉換。 isinstance(x,dict)現在可用。謝謝。 – 2010-09-22 17:58:41

相關問題