简体   繁体   English

python 2.5是否等效于Tcl的上层命令?

[英]does python 2.5 have an equivalent to Tcl's uplevel command?

Does python have an equivalent to Tcl's uplevel command? python是否等效于Tcl的上层命令? For those who don't know, the "uplevel" command lets you run code in the context of the caller. 对于那些不知道的人,“ uplevel”命令使您可以在调用者的上下文中运行代码。 Here's how it might look in python: 这是它在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. 我希望能够执行任何代码。

In general, what you ask is not possible (with the results you no doubt expect). 通常,您要求的是不可能的(毫无疑问,您期望得到的结果)。 Eg, imagine the "any code" is x = 23 . 例如,假设“任何代码”是x = 23 Will this add a new variable x to your caller's set of local variables, assuming you do find a black-magical way to execute this code "in the caller"? 假设您确实发现“在调用方中”执行此代码的黑魔法方法,这是否会在调用方的局部变量集中添加新变量x No it won't -- the crucial optimization performed by the Python compiler is to define once and for all, when def 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. 不,它不会-Python编译器执行关键优化是一劳永逸地定义,当def执行时,精确定义一组局部变量(在函数体内分配或绑定的所有裸名) ,然后将对这些裸名的所有访问和设置转换为非常快速的索引到堆栈框架中。 (You could systematically defeat that crucial optimization eg by having an exec '' at the start of every possible caller -- and see your system's performance crash through the floor in consequence). (您可以系统地挫败该关键的优化,例如,在每个可能的调用者开始时都使用exec '' -从而导致系统性能彻底崩溃)。

Except for assigning to the caller's local barenames, exec thecode in thelocals, theglobals may do roughly what you want, and the inspect 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;-). 除了分配给呼叫者的本地裸名, exec thecode in thelocals, theglobals本地exec thecode in thelocals, theglobals ,全局变量可能会大致执行您想要的操作,而inspect模块可让您以一种半合理的方式获取呼叫者的本地变量和全局变量(就深黑色魔术而言) -会让我发邮件通知任何建议其在生产代码中使用的同事-可以用称其为“半合理”的方式得到应有的称赞,即;-)。

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. 明确规范的唯一解决方案(!同时感谢您如此精确,因为它使回答更简单)是:然后,使用不同的编程语言。

Is the third party library written in Python? 第三方库是用Python编写的吗? If yes, you could rewrite and rebind the function "foo" at runtime with your own implementation. 如果是,则可以在运行时使用您自己的实现重写并重新绑定函数“ foo”。 Like so: 像这样:

import third_party

original_foo = third_party.foo
def my_foo(*args, **kwds):
    # do your magic...
    original_foo(*args, **kwds)
third_party.foo = my_foo

I guess monkey-patching is slighly better than rewriting frame locals. 我猜猴子修补比重写框架本地修补要好得多。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM