Mainframe_Wanderer
Mainframe_Wanderer

Reputation: 1

Python hash runtime

I have a runtime question.

In the Python function:

 def f(x):
     hash = { 1: g(x) }
     return hash[1]

when is g(x) actually executed? Is it when f(x) is called or when hash[1] is returned?

Upvotes: 0

Views: 90

Answers (2)

yejinxin
yejinxin

Reputation: 596

agree with Matthew. More exactly, g(x) is executed when hash object is created in f(x).

Upvotes: 0

Matthew Adams
Matthew Adams

Reputation: 10156

g(x) is executed when f(x) is called; g(x) is called when hash is made.

If you remove the return statement, you can see what's happening:

>>> def g(x):
...     print 'g(%s) called' % x
... 
>>> def f(x):
...     hash = { 1: g(x)}
... 
>>> f(1)
g(1) called

Upvotes: 3

Related Questions