4 ms·
A simple fix to the time/now example is also useful for demonstrating first-class functions: def print_now(now=time.time): print now()
by nknighthb 13y ago
A simple fix to the time/now example is also useful for demonstrating first-class functions:
def print_now(now=time.time):
print now()
- DrJ 13y agoif I recall, this is how SQLAlchemy does default values that are functions.
- ajanuary 13y agoYou do then have to wrap values in lambdas if you want to pass them in.
- herge 13y agoThe classic way is: def print_now(now=None): now = now or time.time() print now
- mnordhoff 13y agoI hope "now" is never 1970-01-01 00:00:00. Edit: if now is None: now = time.time()
- herge 13y agoOr even more paranoid: import mock def print_now(now=mock.sentinel.now): now = now if now != mock.sentinel.now else time.time()
- mnordhoff 13y agoI don't have experience with mock, but I've used object() -- literally -- as a sentinel. _sentinel = object() def print_now(now=_sentinel): if now is _sentinel: now = time.time() ...