美文网首页
代码对象 code object 与 __code__ 属性

代码对象 code object 与 __code__ 属性

作者: huashen_9126 | 来源:发表于2020-04-20 23:45 被阅读0次

    定义

    代码对象 code object 是一段可执行的 Python 代码在 CPython 中的内部表示。

    def test():
        pass
    
    co_list = []
    L = dir(test.__code__)
    for l in L:
        if not l.startswith('__'):
            co_list.append(l)
    
    print(co_list)
    
    属性 描述
    co_argcount number of arguments (not including keyword only arguments, * or ** args)
    co_code string of raw compiled bytecode
    co_cellvars tuple of names of cell variables (referenced by containing scopes)
    co_consts tuple of constants used in the bytecode
    co_filename name of file in which this code object was created
    co_firstlineno number of first line in Python source code
    co_flags bitmap of CO_* flags, read more here
    co_lnotab encoded mapping of line numbers to bytecode indices
    co_freevars tuple of names of free variables (referenced via a function’s closure)
    co_kwonlyargcount number of keyword only arguments (not including ** arg)
    co_name name with which this code object was defined
    co_names tuple of names of local variables
    co_nlocals number of local variables
    co_stacksize virtual machine stack space required
    co_varnames tuple of names of arguments and local variables

    参考资料:
    https://blog.csdn.net/jpch89/article/details/86764245

    相关文章

      网友评论

          本文标题:代码对象 code object 与 __code__ 属性

          本文链接:https://www.haomeiwen.com/subject/pbfeihtx.html