5 ms·
import datetime, time def foo2(): def foo(a=datetime.datetime.now()): time.sleep(1) return a return foo() for n in range(10): print foo
by kghose 12y ago
import datetime, time
def foo2():
def foo(a=datetime.datetime.now()):
time.sleep(1)
return a
return foo()
for n in range(10): print foo2()
2014-05-08 11:23:01.642871
2014-05-08 11:23:02.644276
2014-05-08 11:23:03.644579
2014-05-08 11:23:04.645146
2014-05-08 11:23:05.646328
2014-05-08 11:23:06.647572
2014-05-08 11:23:07.647904
2014-05-08 11:23:08.648213
2014-05-08 11:23:09.648973
2014-05-08 11:23:10.649742
So, not quite module load time, but evaluation time.
- herge 12y agoYou are technically correct, the best type of correct. The top level function is evaluated at module load time.
- marcosdumay 12y agoKilink's comment apply again. If you define several functions at different times, the default argument will be evaluated each time you define a new function. You'll have the same behaviour if you keep reassigning lambdas at the same function name, or if you keep edditing the globals. You are misunderstanding how dynamic Python is. And, yes, the part about "module load time" was a simplification.