types.py 1.08 KB
Newer Older
1 2
"""
Define names for built-in types that aren't directly accessible as a builtin.
3
"""
4 5
import sys

6 7 8
# 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
9
# "__iter__" and "__next__" attributes instead.
10

Guido van Rossum's avatar
Guido van Rossum committed
11 12
def _f(): pass
FunctionType = type(_f)
13
LambdaType = type(lambda: None)         # Same as FunctionType
14
CodeType = type(_f.__code__)
Guido van Rossum's avatar
Guido van Rossum committed
15

16
def _g():
17
    yield 1
18
GeneratorType = type(_g())
19

Guido van Rossum's avatar
Guido van Rossum committed
20
class _C:
21
    def _m(self): pass
22
MethodType = type(_C()._m)
Guido van Rossum's avatar
Guido van Rossum committed
23 24

BuiltinFunctionType = type(len)
25
BuiltinMethodType = type([].append)     # Same as BuiltinFunctionType
26 27 28 29

ModuleType = type(sys)

try:
30
    raise TypeError
31
except TypeError:
32 33 34
    tb = sys.exc_info()[2]
    TracebackType = type(tb)
    FrameType = type(tb.tb_frame)
35
    tb = None; del tb
36

Christian Heimes's avatar
Christian Heimes committed
37 38 39
# For Jython, the following two types are identical
GetSetDescriptorType = type(FunctionType.__code__)
MemberDescriptorType = type(FunctionType.__globals__)
40 41

del sys, _f, _g, _C,                              # Not for export