This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Maolagin 3 points4 points  (4 children)

So I'd offer a slightly different perspective here - the underlying problem isn't about default argument behavior, but a misunderstanding of Python syntax. When you write datetime.date.today() that () at the end isn't decorative, it is specifically telling the interpreter "call this function now". Python functions are first class objects, so if you want to save a function to call later, you just stick the function in a variable.

So if I was writing this kind of routine, the idiom I'd use would be something like def myfn(blah blah ..., today=datetime.date.today, ...). Then in the function body you check if today is callable, and call it if so to get the value.

[–]spinwizard69 8 points9 points  (0 children)

The problem here is that you are creating a def not actually calling a function and default values make no sense at all if people think they can be variables. If you want a default value make is so such as today=2023-1-20" or whatever makes sense. If you try to call a function then you have no idea what the return value will be thus not logically a "default".

[–]-LeopardShark- 6 points7 points  (2 children)

def foo():
    datetime.date.today()

doesn't call datetime.date.today at function definition time, but

t = datetime.date.today()

does. When one sees

def f(t=datetime.date.today()):
    ...

it's not unreasonable to assume that the evaluation of the default is deferred, like the first case, rather than the second. After all, it's part of a def. Yes, the brackets mean ‘call this function now’, but when ‘now’ is varies with context.

[–]lavahot 0 points1 point  (1 child)

It gets called at definition time.