types.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Define names for all type symbols known in the standard interpreter.
  2. Types that are part of optional modules (e.g. array) are not listed.
  3. """
  4. import sys
  5. # Iterators in Python aren't a matter of type but of protocol. A large
  6. # and changing number of builtin types implement *some* flavor of
  7. # iterator. Don't check the type! Use hasattr to check for both
  8. # "__iter__" and "next" attributes instead.
  9. NoneType = type(None)
  10. TypeType = type
  11. ObjectType = object
  12. IntType = int
  13. LongType = long
  14. FloatType = float
  15. BooleanType = bool
  16. try:
  17. ComplexType = complex
  18. except NameError:
  19. pass
  20. StringType = str
  21. # StringTypes is already outdated. Instead of writing "type(x) in
  22. # types.StringTypes", you should use "isinstance(x, basestring)". But
  23. # we keep around for compatibility with Python 2.2.
  24. try:
  25. UnicodeType = unicode
  26. StringTypes = (StringType, UnicodeType)
  27. except NameError:
  28. StringTypes = (StringType,)
  29. BufferType = buffer
  30. TupleType = tuple
  31. ListType = list
  32. DictType = DictionaryType = dict
  33. def _f(): pass
  34. FunctionType = type(_f)
  35. LambdaType = type(lambda: None) # Same as FunctionType
  36. CodeType = type(_f.func_code)
  37. def _g():
  38. yield 1
  39. GeneratorType = type(_g())
  40. class _C:
  41. def _m(self): pass
  42. ClassType = type(_C)
  43. UnboundMethodType = type(_C._m) # Same as MethodType
  44. _x = _C()
  45. InstanceType = type(_x)
  46. MethodType = type(_x._m)
  47. BuiltinFunctionType = type(len)
  48. BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
  49. ModuleType = type(sys)
  50. FileType = file
  51. XRangeType = xrange
  52. try:
  53. raise TypeError
  54. except TypeError:
  55. tb = sys.exc_info()[2]
  56. TracebackType = type(tb)
  57. FrameType = type(tb.tb_frame)
  58. del tb
  59. SliceType = slice
  60. EllipsisType = type(Ellipsis)
  61. DictProxyType = type(TypeType.__dict__)
  62. NotImplementedType = type(NotImplemented)
  63. # For Jython, the following two types are identical
  64. GetSetDescriptorType = type(FunctionType.func_code)
  65. MemberDescriptorType = type(FunctionType.func_globals)
  66. del sys, _f, _g, _C, _x # Not for export
  67. __all__ = list(n for n in globals() if n[:1] != '_')