python 2.5 是否有相当于 Tcl 的 uplevel 命令?
python 有相当于 Tcl 的 uplevel 命令吗?对于那些不知道的人,“uplevel”命令允许您在调用者的上下文中运行代码。这是它在 python 中的样子:
def foo():
answer = 0
print "answer is", answer # should print 0
bar()
print "answer is", answer # should print 42
def bar():
uplevel("answer = 42")
然而,它不仅仅是设置变量,所以我并不是在寻找仅仅改变字典的解决方案。我希望能够执行任何代码。
Does python have an equivalent to Tcl's uplevel command? For those who don't know, the "uplevel" command lets you run code in the context of the caller. Here's how it might look in python:
def foo():
answer = 0
print "answer is", answer # should print 0
bar()
print "answer is", answer # should print 42
def bar():
uplevel("answer = 42")
It's more than just setting variables, however, so I'm not looking for a solution that merely alters a dictionary. I want to be able to execute any code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,您所要求的是不可能的(毫无疑问,您所期望的结果)。例如,假设“任何代码”是
x = 23
。假设您确实找到了一种“在调用者中”执行此代码的神奇方法,这是否会向调用者的局部变量集中添加一个新变量x
?不,它不会——Python编译器执行的关键优化是在def
执行时一劳永逸地定义一组精确的局部变量(所有在函数体中分配或以其他方式绑定的裸名),并将对这些裸名的每次访问和设置转变为对堆栈帧的快速索引。 (您可以系统地击败这个关键的优化,例如通过在每个可能的调用者开始时使用exec''
- 并因此看到系统性能崩溃) 。除了分配给调用者的本地裸名之外,
执行 thelocals 中的代码,theglobals
可能会大致执行您想要的操作,并且inspect
模块可以让您得到调用者的局部变量和全局变量以半合理的方式(就深层黑魔法而言——这会让我对任何建议在生产代码中犯下这种行为的同事进行邮寄——可以得到不值得的赞扬称之为“半合理”,即;-)。但您确实指定了“我希望能够执行任何代码”。对于这个明确的规范(感谢您如此精确,因为它使回答更容易!)的唯一解决方案是:然后,使用不同的编程语言。
In general, what you ask is not possible (with the results you no doubt expect). E.g., imagine the "any code" is
x = 23
. Will this add a new variablex
to your caller's set of local variables, assuming you do find a black-magical way to execute this code "in the caller"? No it won't -- the crucial optimization performed by the Python compiler is to define once and for all, whendef
executes, the exact set of local variables (all the barenames that get assigned, or otherwise bound, in the function's body), and turn every access and setting to those barenames into very fast indexing into the stackframe. (You could systematically defeat that crucial optimization e.g. by having anexec ''
at the start of every possible caller -- and see your system's performance crash through the floor in consequence).Except for assigning to the caller's local barenames,
exec thecode in thelocals, theglobals
may do roughly what you want, and theinspect
module lets you get the locals and globals of the caller in a semi-reasonable way (in as far as deep black magic -- which would make me go postal on any coworker suggesting it be perpetrated in production code -- can ever be honored with the undeserved praise of calling it "semi-reasonable", that is;-).But you do specify "I want to be able to execute any code." and the only solution to that unambiguous specification (and thanks for being so precise, as it makes answering easier!) is: then, use a different programming language.
第三方库是Python写的吗?如果是,您可以在运行时使用您自己的实现重写并重新绑定函数“foo”。就像这样:
我想猴子修补比重写框架局部变量要好一些。 ;)
Is the third party library written in Python? If yes, you could rewrite and rebind the function "foo" at runtime with your own implementation. Like so:
I guess monkey-patching is slighly better than rewriting frame locals. ;)