types.py 2.1 KB
Newer Older
1 2 3 4
"""Define names for all type symbols known in the standard interpreter.

Types that are part of optional modules (e.g. array) are not listed.
"""
5 6
import sys

7 8 9 10 11
# Iterators in Python aren't a matter of type but of protocol.  A large
# and changing number of builtin types implement *some* flavor of
# iterator.  Don't check the type!  Use hasattr to check for both
# "__iter__" and "next" attributes instead.

12
NoneType = type(None)
13 14
TypeType = type
ObjectType = object
15

16 17 18
IntType = int
LongType = long
FloatType = float
Skip Montanaro's avatar
Skip Montanaro committed
19
BooleanType = bool
20
try:
21
    ComplexType = complex
22
except NameError:
23
    pass
24

25
StringType = str
26 27 28 29

# StringTypes is already outdated.  Instead of writing "type(x) in
# types.StringTypes", you should use "isinstance(x, basestring)".  But
# we keep around for compatibility with Python 2.2.
30 31
try:
    UnicodeType = unicode
32
    StringTypes = (StringType, UnicodeType)
33
except NameError:
34
    StringTypes = (StringType,)
35

36
BufferType = buffer
37

38
TupleType = tuple
39
ListType = list
40
DictType = DictionaryType = dict
41

Guido van Rossum's avatar
Guido van Rossum committed
42 43
def _f(): pass
FunctionType = type(_f)
44
LambdaType = type(lambda: None)         # Same as FunctionType
45 46
try:
    CodeType = type(_f.func_code)
47 48
except RuntimeError:
    # Execution in restricted environment
49
    pass
Guido van Rossum's avatar
Guido van Rossum committed
50

51 52 53 54 55
def g():
    yield 1
GeneratorType = type(g())
del g

Guido van Rossum's avatar
Guido van Rossum committed
56
class _C:
57
    def _m(self): pass
Guido van Rossum's avatar
Guido van Rossum committed
58
ClassType = type(_C)
59
UnboundMethodType = type(_C._m)         # Same as MethodType
Guido van Rossum's avatar
Guido van Rossum committed
60 61 62 63 64
_x = _C()
InstanceType = type(_x)
MethodType = type(_x._m)

BuiltinFunctionType = type(len)
65
BuiltinMethodType = type([].append)     # Same as BuiltinFunctionType
66 67

ModuleType = type(sys)
68
FileType = file
69
XRangeType = xrange
70 71

try:
72
    raise TypeError
73
except TypeError:
74
    try:
75 76 77
        tb = sys.exc_info()[2]
        TracebackType = type(tb)
        FrameType = type(tb.tb_frame)
78 79 80
    except AttributeError:
        # In the restricted environment, exc_info returns (None, None,
        # None) Then, tb.tb_frame gives an attribute error
81
        pass
82
    tb = None; del tb
83

84
SliceType = slice
85
EllipsisType = type(Ellipsis)
86

87
DictProxyType = type(TypeType.__dict__)
88
NotImplementedType = type(NotImplemented)
89

90
del sys, _f, _C, _x                  # Not for export