Detect types in python2

 1 >>> type('bonjour')
 2 <type 'str'>
 3 >>> type(type('bonsoir'))
 4 <type 'type'>
 5 >>> dir(_)
 6 ['__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__call__', '__class__', '__delattr__', '__dict__', '__dictoffset__', '__doc__', '__eq__', '__flags__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__instancecheck__', '__itemsize__', '__le__', '__lt__', '__module__', '__mro__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasscheck__', '__subclasses__', '__subclasshook__', '__weakrefoffset__', 'mro']
 7 >>> type(2**100).__name__
 8 'long'
 9 >>> if isinstance('what up', (complex, float, int, long, str)): True
10 
11 True
12 >>> type(123) == type(23333333333333333)
13 False
14 >>> type(123) == type(233)
15 True

There is also a module called types.

1 >>> import types
2 >>> dir(types)
3 ['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'CodeType', 'ComplexType', 'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType', 'FileType', 'FloatType', 'FrameType', 'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'InstanceType', 'IntType', 'LambdaType', 'ListType', 'LongType', 'MemberDescriptorType', 'MethodType', 'ModuleType', 'NoneType', 'NotImplementedType', 'ObjectType', 'SliceType', 'StringType', 'StringTypes', 'TracebackType', 'TupleType', 'TypeType', 'UnboundMethodType', 'UnicodeType', 'XRangeType', '__builtins__', '__doc__', '__file__', '__name__', '__package__']

Three key properties of all python objects: (ID, TYPE, VALUE)

 

你可能感兴趣的:(Detect types in python2)