也许这个问题很容易回答,但经过长时间的互联网搜索,我发现没有回答/解决方法:
在Python(v3)中,我想做以下事情:
def func():
pass
foo = "bar"
# Now I would like to make an assignment to a variable named
# based on the value of foo (i.e. effectively bar = func, but using foo)
# so that:
bar() == func() # is True
bar is func # is also True
我该怎么办呢? 谢谢!
答案 0 :(得分:1)
我在猜测问题是什么,但是你在找这样的东西:
def func():
pass
foo = "bar"
locals()[foo] = func
bar() == func() # True
# or better yet
bar is func # also True