Special read-only attributes:

  • #co_name gives the function name;
  • #co_argcount is the number of positional arguments (including arguments with default values);
  • #co_nlocals is the number of local variables used by the function (including arguments);
  • #co_varnames is a tuple containing the names of the local variables (starting with the argument names);
  • co_cellvars is a tuple containing the names of local variables that are referenced by nested functions;
  • co_freevars is a tuple containing the names of free variables;
  • co_code is a string representing the sequence of bytecode instructions;
  • #co_consts is a tuple containing the literals used by the bytecode;
  • co_names is a tuple containing the names used by the bytecode;
  • #co_filename is the filename from which the code was compiled;
  • #co_firstlineno is the first line number of the function;
  • co_lnotab is a string encoding the mapping from bytecode offsets to line numbers (for details see the source code of the interpreter);
  • co_stacksize is the required stack size (including local variables);
  • co_flags is an integer encoding a number of flags for the interpreter.
>>> do(2)
>>> # -------------------------------------------------- step02
# ---------------------------------------- c.co_*
>>> c.co_cellvars
()
>>> c.co_code
b'x\x1f\x00|\x00\x00|\x01\x00k\x00\x00r!\x00|\x00\x00V\x01|\x00\x00|\x02\x007}\x00\x00q\x03\x00Wd\x00\x00S'
>>> c.co_flags
115
>>> c.co_freevars
()
>>> c.co_kwonlyargcount
0
>>> c.co_lnotab
b'\x00\x01\x03\x00\x0c\x01\x05\x01'
>>> c.co_names
()
>>> c.co_stacksize
2
>>> 

組み込み関数:dir

>>> f = add
>>> dir(f)

任意のオブジェクトに関する属性リストが得られます。ここでは、関数オブジェクト f について、

  • 有効な属性として、__class__, __doc__, __name__ などが含まれるのが分かります。

組み込み関数:type

>>> type(f)

任意のオブジェクトに関する型情報が得られます。ここでは、関数オブジェクト f について、

  • その型(クラス)が function だと分かります。

関数:文書化文字列

    def add(a,b):
        "implement the binary arithmetic operations"
        return a+b

関数の本体の先頭行には、文書化文字列を記述できます。

    
'implement the binary arithmetic operations'
属性 __doc__ を参照すると、文書化文字列として記述した内容が得られます。