When should default function arguments be evaluated?

I was writing my first app and I found a bug. The date time column in my database always had the same value. I found out that this value changed upon restarting the server.

This was the problematic piece of code:

def foo(param, now=datetime.now()):
    do stuff

The problem was python was evaluating this argument only once to be precise when it found the declaration.

To put my observation to test, I wrote this sample program:

def foo(now=datetime.now()):
    print(now)

foo()
sleep(1)
foo()

The output was

2020-08-16 12:29:11.703077
2020-08-16 12:29:11.703077

I tried this in C++ with this code to do this exact same thing:

#include <iostream>
#include <chrono>

void foo(std::chrono::system_clock::time_point now = std::chrono::high_resolution_clock::now()) {
    std::cout << now.time_since_epoch().count() << "\n";
}

int main() {
    foo();
    foo();
}

And this was the ouput

1597497303053213767
1597497303053237417

It just worked! It evaluated default arguments when the function was called. I could not test it in java because it does not support default arguments. This got me thinking, what are the benefits of either approach and why did the languages make their respective choice? To be honest, I don't know so comment down your thoughts!

P.S. The python program I wrote to test my code was not my original test program. The new program was suggested by Shiv Danyal which expresses my intent better than my original program

P.P.S Refer to these docs for further read:

  1. stackoverflow.com/questions/1132941/least-a..
  2. effbot.org/zone/default-values.htm