甩祸 发表于 2018-8-7 11:23:27

检查Python对象

>>> import types  
>>> print types.__doc__
  
Define names for all type symbols known in the standard interpreter.
  
Types that are part of optional modules (e.g. array) are not listed.
  
>>> dir(types)
  
['BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType',
  
'CodeType', 'ComplexType', 'DictProxyType', 'DictType', 'DictionaryType',
  
'EllipsisType', 'FileType', 'FloatType', 'FrameType', 'FunctionType',
  
'GeneratorType', 'InstanceType', 'IntType', 'LambdaType', 'ListType',
  
'LongType', 'MethodType', 'ModuleType', 'NoneType', 'ObjectType', 'SliceType',
  
'StringType', 'StringTypes', 'TracebackType', 'TupleType', 'TypeType',
  
'UnboundMethodType', 'UnicodeType', 'XRangeType', '__builtins__', '__doc__',
  
'__file__', '__name__']
  
>>> s = 'a sample string'
  
>>> type(s)
  
<type 'str'>
  
>>> if type(s) is types.StringType: print "s is a string"
  
...
  
s is a string
  
>>> type(42)
  
<type 'int'>
  
>>> type([])
  
<type 'list'>
  
>>> type({})
  
<type 'dict'>
  
>>> type(dir)
  
<type 'builtin_function_or_method'>
  
页: [1]
查看完整版本: 检查Python对象