1

I have a Helper.py that looks like this:

def toDayDate():
   return Now()    

def getAge(dob,today=toDayDate()):
  doMagic()

then I use it here:

from helper import getAge

input=getDOBfromUSER()
getAge(input)

The problem is when the Import is being interpreted by python the toDayDate() is run anyway!!!

What am I doing wrong here?

The above set up is so argument is set to a default dynamic

Emile
  • 345
  • 2
  • 11
Max
  • 4,152
  • 4
  • 36
  • 52
  • can you paste the complete `helper.py` file for better context? I suspect you may need to nest any function calls in the script inside the main. – rm -f me Sep 28 '21 at 09:40
  • 1
    The default value for the `today` parameter in the `getAge` function is the result of a call to `toDayDate` – Iain Shelvington Sep 28 '21 at 09:41
  • The issue I have is, that the method call in the definication is being called even when the method is not even being called. – Max Sep 28 '21 at 09:43

2 Answers2

2

You might want to read on the following resources:

When you declare it like this:

def getAge(dob,today=toDayDate()):

Quoting from those references:

Python’s default arguments are evaluated once when the function is defined, not each time the function is called

Let's prove it:

>>> from datetime import datetime
>>> 
>>> 
>>> datetime.now()  # Display the time before we define the function
datetime.datetime(2021, 9, 28, 17, 54, 16, 761492)
>>>
>>> def func(var=datetime.now()):  # Set the time to now
...     print(var)
... 
>>> func()
2021-09-28 17:54:16.762774
>>> func()
2021-09-28 17:54:16.762774
>>> func()
2021-09-28 17:54:16.762774

As you can see, even if we call func() after a few minutes, hours, or days, its value will be fixed to the time when we defined it, and wouldn't actually change upon every call. Also this proves that once you defined a function (or imported a file containing that function), its definition would already be assessed, which includes its default arguments. It wouldn't wait for you to call the function first before setting the default arguments.

What you need to do is:

def getAge(dob,today=None):
  if today is None:
    today = toDayDate()
  doMagic()

Or perhaps you can take advantage of boolean short-circuiting:

def getAge(dob,today=None):
  today = today or toDayDate()
  doMagic()
1

The default value of a parameter is evaluated at definition time in Python. To effectively make a default value evaluated at run time, a common approach is to set the default value to None and then use a condition to set it to a dynamically calculated value inside the function:

def getAge(dob, today=None):
    if today is None:
        today = toDayDate()
    doMagic()
blhsing
  • 91,368
  • 6
  • 71
  • 106