1
from datetime import datetime
from time import sleep


def a1(v=datetime.utcnow()):
    print(v)


if __name__ == '__main__':
    a1()
    sleep(1)
    a1()
result:
2020-07-06 11:26:31.959986
2020-07-06 11:26:31.959986

When calling a function that has for default parameter a method call, why is the result always the same?

shouldn't be executed every time the method is called?

Pedro Nunes
  • 367
  • 2
  • 5
  • it will not execute everytime @pedro Nunes – Vignesh Jul 06 '20 at 11:48
  • `def` statements are executed once. This question has some good explanations as to why that is. https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument – Axe319 Jul 06 '20 at 11:52

0 Answers0