"""This module includes tests of the code object representation.>>> def f(x):... def g(y):... return x + y... return g...>>> dump(f.func_code)name: fargcount: 1names: ()varnames: ('x', 'g')cellvars: ('x',)freevars: ()nlocals: 2flags: 3consts: ('None', '<code object g>')>>> dump(f(4).func_code)name: gargcount: 1names: ()varnames: ('y',)cellvars: ()freevars: ('x',)nlocals: 1flags: 19consts: ('None',)>>> def h(x, y):... a = x + y... b = x - y... c = a * b... return c...>>> dump(h.func_code)name: hargcount: 2names: ()varnames: ('x', 'y', 'a', 'b', 'c')cellvars: ()freevars: ()nlocals: 5flags: 67consts: ('None',)>>> def attrs(obj):... print obj.attr1... print obj.attr2... print obj.attr3>>> dump(attrs.func_code)name: attrsargcount: 1names: ('attr1', 'attr2', 'attr3')varnames: ('obj',)cellvars: ()freevars: ()nlocals: 1flags: 67consts: ('None',)>>> def optimize_away():... 'doc string'... 'not a docstring'... 53... 53L>>> dump(optimize_away.func_code)name: optimize_awayargcount: 0names: ()varnames: ()cellvars: ()freevars: ()nlocals: 0flags: 67consts: ("'doc string'", 'None')"""def consts(t):"""Yield a doctest-safe sequence of object reprs."""for elt in t:r = repr(elt)if r.startswith("<code object"):yield "<code object %s>" % elt.co_nameelse:yield rdef dump(co):"""Print out a text representation of a code object."""for attr in ["name", "argcount", "names", "varnames", "cellvars","freevars", "nlocals", "flags"]:print "%s: %s" % (attr, getattr(co, "co_" + attr))print "consts:", tuple(consts(co.co_consts))def test_main(verbose=None):from test.test_support import run_doctestfrom test import test_coderun_doctest(test_code, verbose)if __name__ == '__main__':test_main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。