0

Why does this:

from datetime import datetime
from time import sleep,time

def test_func(tmp=datetime.now()):
    print(tmp)

test_func()
sleep(10)
test_func()

..result in this:

2015-02-16 17:06:28.283351
2015-02-16 17:06:28.283351

..when I would expect this:

2015-02-16 17:06:28.283351
2015-02-16 17:06:38.283351

**Edit - changed the sleep to 10 seconds and the result is the same.

Jon Cage
  • 36,366
  • 38
  • 137
  • 215
  • To the down-voter / vote-to-closer: what is unclear about that? – Jon Cage Feb 16 '15 at 17:13
  • This is indeed a duplicate of that question. I couldn't find anything similar when I searched. Robert Lifrido's answer says it all: http://stackoverflow.com/a/1145781/15369 – Jon Cage Feb 16 '15 at 17:19
  • when the entire module is loaded first, datetime.now() is resolved to whatever datetime.now() was at that time. so more or less it is saying `def test_func(tmp='2015-02-16blablabla')` – TehTris Feb 16 '15 at 18:03

0 Answers0