I am viewing python epiphanies by O'Reilly media and they gave this as an example
def log(severity,message):
print("{0}:{1}".format(severity.upper(),message))
def create_logger(severity):
def logger(message):
log(severity,message)
return logger
warning2 = create_logger("warning")
warning2("this is a warning")
output:
WARNING:this is a warning
I knew bit about decorators and the format of create_logger is exactly like a decorator except it's not being used as one.
what boggles my mind is
this line warning2("this is a warning")
warning2 contains the create_logger function with an argument of "warning"
so it's basically getting called already
and doing warning2("this is a warning") is like calling it twice?
I did however notice that doing:
create_logger("warning")("this is a warning")
yields the same results
which would lead me to believe that the second call i.e ("this is a warning")
is implicitly referencing the logger function inside create_logger and passing the arguments to it?
am I understanding it right? and when would we ever need to use this?