python 2.5 是否有相当于 Tcl 的 uplevel 命令?

发布于 2024-09-25 01:56:41 字数 344 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兲鉂ぱ嘚淚 2024-10-02 01:56:41

一般来说,您所要求的是不可能的(毫无疑问,您所期望的结果)。例如,假设“任何代码”是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 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"? 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. (You could systematically defeat that crucial optimization e.g. by having an exec '' 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 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;-).

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.

乜一 2024-10-02 01:56:41

第三方库是Python写的吗?如果是,您可以在运行时使用您自己的实现重写并重新绑定函数“foo”。就像这样:

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

我想猴子修补比重写框架局部变量要好一些。 ;)

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:

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. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文