Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个运行时问题。
在 Python 函数中:
def f(x): hash = { 1: g(x) } return hash[1]
g(x) 何时实际执行?是在调用 f(x) 时还是返回 hash[1] 时?
g(x)f(x)被调用时执行;g(x)在制作时调用hash。
g(x)
f(x)
hash
如果您删除 return 语句,您可以看到发生了什么:
>>> def g(x): ... print 'g(%s) called' % x ... >>> def f(x): ... hash = { 1: g(x)} ... >>> f(1) g(1) called
同意马修。更准确地说,在创建对象g(x)时执行。hashf(x)