如果要修改多个no inputs no outputs函数,请查看types.CodeType构造函数:
>>> import types
>>> help(types.CodeType)
Help on class code in module builtins:
class code(object)
| code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,
| constants, names, varnames, filename, name, firstlineno,
| lnotab[, freevars[, cellvars]])
|
| Create a code object. Not for the faint of heart.
| ...
这与编写汇编代码很相似,可能会导致seg错误、python解释器崩溃甚至更糟。你知道当唯一的文档是Not for the faint of heart.时,开发人员并不打算直接使用它。你知道吗
如果您想操作
foo
的定义,可以直接用global
语句和=
来重新定义foo
与分配任何其他全局变量的方式非常相似:只在
def foo(): ...
内部foo2
而不是定义replacement_foo
然后赋值也可能是安全的,但这就是officially not permitted,即使实现没有强制执行它。你知道吗在注释中,您似乎对通过堆栈操纵
foo
非常感兴趣。foo
的定义不在堆栈上,在执行foo
期间,堆栈上没有对foo
的调用,因此堆栈是错误的查找位置。你知道吗另外,Python的大多数堆栈检查都是只读的。即使要重新分配函数的code对象,它也不会更改堆栈上已有的调用代码。您不能更改
main
的代码对象,使其不调用foo
(尽管您可以更改foo
的代码对象,这将在main
调用foo
时生效)。你知道吗如果您对python中的字节码级内容感兴趣,让我来介绍
dis
、compile
和CodeType
不要在生产代码中执行这些操作
您可以将
compile
python代码(作为字符串)转换为code对象,然后该对象可以重写functions__code__
属性如果要修改多个no inputs no outputs函数,请查看
types.CodeType
构造函数:这与编写汇编代码很相似,可能会导致seg错误、python解释器崩溃甚至更糟。你知道当唯一的文档是
Not for the faint of heart.
时,开发人员并不打算直接使用它。你知道吗如果您想查看实际执行的字节码,可以使用
dis
。是“拆卸”的缩写看看python代码实际上是如何执行的,这很有趣。你知道吗
我不确定您到底想用这个堆栈做什么,但我相当肯定它到底是什么,python的开发人员已经确保它不容易,因为这在python中不是您应该接触的东西。这些类型只存在于python中是为了内省(比如通过检查函数的调用签名来查看函数在运行时的参数)
相关问题 更多 >
编程相关推荐