. , , , , , , , .
, , -. , , . , :
>>> def fib(i):
... x, y = 0, 1
... for _ in range(i):
... x, y = y, x+y
... return x
...
>>> fib.__code__.co_code
b'd\x03\\\x02}\x01}\x02x\x1et\x00|\x00\x83\x01D\x00]\x12}\x03|\x02|\x01|\x02\x17\x00\x02\x00}\x01}\x02q\x1
2W\x00|\x01S\x00'
... .
- Python , . dis, - :
>>> import dis
>>> dis.dis(fib)
2 0 LOAD_CONST 3 ((0, 1))
2 UNPACK_SEQUENCE 2
4 STORE_FAST 1 (x)
6 STORE_FAST 2 (y)
3 8 SETUP_LOOP 30 (to 40)
10 LOAD_GLOBAL 0 (range)
12 LOAD_FAST 0 (i)
14 CALL_FUNCTION 1
16 GET_ITER
>> 18 FOR_ITER 18 (to 38)
20 STORE_FAST 3 (_)
4 22 LOAD_FAST 2 (y)
24 LOAD_FAST 1 (x)
26 LOAD_FAST 2 (y)
28 BINARY_ADD
30 ROT_TWO
32 STORE_FAST 1 (x)
34 STORE_FAST 2 (y)
36 JUMP_ABSOLUTE 18
>> 38 POP_BLOCK
5 >> 40 LOAD_FAST 1 (x)
42 RETURN_VALUE
, , ALL_CAPS .
ALL_CAPS -. , LOAD_CONST , BINARY_ADD - +. -. , LOAD_CONST 3 , 3 . , - -. , , , 3 LOAD_CONST 3 (0, 1) 1 STORE_FAST 1 x, -; .
- , , dict, , , , , __name__.
Python 3.6 C:
typedef struct {
PyObject_HEAD
PyObject *func_code;
PyObject *func_globals;
PyObject *func_defaults;
PyObject *func_kwdefaults;
PyObject *func_closure;
PyObject *func_doc;
PyObject *func_name;
PyObject *func_dict;
PyObject *func_weakreflist;
PyObject *func_module;
PyObject *func_annotations;
PyObject *func_qualname;
} PyFunctionObject;
, ,
- dict,
- ,
- ,
- ,
- docstring,
- ,
__dict__,- ,
__module__,__qualname__,
PyObject_HEAD refcount/GC.
C, - dir , Python, , , .
, , , . .
bytestring - Python. . , , , , , , . - , . , , , , co_flags ( ) co_stacksize ( , ), .