I would like to have a function as an optional argument of another function in python but it is not clear for me how I can do that.
For example I define the following function:
import os, time, datetime
def f(t=datetime.datetime.now()):
return t.timetuple()
I have placed t=datetime.datetime.now()
in order for the argument to be optional so
to be able to call f() with no arguments.
Now whenever in time I execute f() I get the same datetime A (which is wrong according to what I expect), but whenever in time I execute f(datetime.datetime.now()) I get different datetimes (which is correct as expected).
For example
>>> f()
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=20, tm_hour=15, tm_min=36, tm_sec=2, tm_wday=5, tm_yday=171, tm_isdst=-1)
>>> f()
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=20, tm_hour=15, tm_min=36, tm_sec=2, tm_wday=5, tm_yday=171, tm_isdst=-1)
>>> f(datetime.datetime.now())
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=20, tm_hour=15, tm_min=37, tm_sec=1, tm_wday=5, tm_yday=171, tm_isdst=-1)
>>> f()
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=20, tm_hour=15, tm_min=36, tm_sec=2, tm_wday=5, tm_yday=171, tm_isdst=-1)
So why the fourth call returns me back to min 36 and sec 2 while the call was made before that? Why the first two calls give the same exact time even if I let plenty of time between them?