这可能看起来像一个相当奇怪的事情,但我很好奇,如果有可能隐式通过一个变量在Python中调用链而不传递它作为参数。为了更好地说明这里有一个例子:Python中的每个激活“全局”变量?
这里是 “正常” 的方式:
def three(something):
print(something)
def two(something):
# ...
three(something)
def one(something):
# ...
two(something)
这里就是我希望能够做到:
def three():
# something is defined implicitly
print(something)
def two():
# ...
three()
def one(something):
# somehow define something inside a context
# for this activation
two()
为此,one
,two
和three
不在同一个类或甚至相同的模块。
如果它们不在同一个类或模块中,那么当'three'被调用时定义的'something'有什么保证? – StoryTeller 2013-02-22 20:45:58
什么都没有。这是非常可怕的黑客攻击,但我仍然希望它能够工作。 – 2013-02-22 20:47:38
不,你不知道。你可能会对好奇心或某事感兴趣,但是你不想真的写这样的代码;-) FWIW我认为如果不深入改变语言是不可能的(特别是静态确定哪些名字被查找的范围规则在哪个范围内)。 – delnan 2013-02-22 20:50:34