Thank you for the explanation, and for the time you have spent on it. I think I understand the theory:
if I have a function:
def function_one(x, y, z):
return x + y + z
I can assign that function to a variable:
thesum = function_one
And I can assign the same function to another variable:
theothersum = function_one
While at the same time I can also assign other values, like strings, integers, etc to those variables
thesum = 'hello world'
theothersum = 1
Or if I use parentheses and some parameters I get the output from the function:
thesum = function_one(x, y, z)
And, assuming x, y and z were integers type(thesum)
would output <class 'int'>
.
Which is what is relevant to generate_filename
I suppose what you can also do (amongst other things) is have another function function_two
, that you can also assign to thesum
or theothersum
. So effectively you have the same function able to be referenced by different names, different functions referenced by the same name, the vars just representing fixed values and combinations thereof.
From there I think I am struggling to see what might be the practical purpose. I can possibly see that assigning the function to a variable might mean that you do not repeat code that “sorts” the output according to what is received, as generate_filename does.
I think I am also struggling to understand why you’d want a function that is set at initialisation time, and then not callable/updateable on demand. For example:
>>> from datetime import datetime
>>> from datetime import timezone
>>> def the_time():
... return datetime.now(timezone.utc)
...
>>> print(the_time)
<function the_time at 0x63ef60>
>>> type(the_time)
<class 'function'>
>>> print(the_time())
2020-11-01 21:56:36.867347+00:00
>>> print(the_time())
2020-11-01 21:56:39.237433+00:00
>>>
So, here, each time I call the_time()
I get an updated time string.