Here is a sample decorator:
def smart_divide(func):
def inner(a,b):
print("I am going to divide",a,"and",b)
if b == 0:
print("Whoops! cannot divide")
return
return func(a,b)
return inner
@smart_divide
def divide(a,b):
return a/b
If func
is an object then how do the variables a
and b
get accessed from it?
Isn't it like trying to to do this?
def func(potato):
print(y, x)
Is there a fundamental concept I am not getting? Is what is happening here part of some pattern in Python or is it a special case situation where a
and b
know were to look because it is a generator?
Update
New example from another stack exchange answer
def my_shiny_new_decorator(a_function_to_decorate):
def the_wrapper_around_the_original_function():
print("Before the function runs")
a_function_to_decorate()
print("After the function runs")
return the_wrapper_around_the_original_function
def a_stand_alone_function():
print("I am a stand alone function, don't you dare modify me")
Generators the manual way
a_stand_alone_function = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function()
Generators the proper way
@my_shiny_new_decorator
def another_stand_alone_function():
print("Leave me alone")
According to the place where I got the new answer from the 'manual' way and the 'proper way' are the same .
I think this example may have caused me to get stuck as I was trying to extend it to when there were parameters involved.
I now realise that what I was imagining didn't make sense
I thought that the original code I posted was equivalent to this
divide = smart_divide(divide(a,b))
which if executed would look like this
def smart_divide(divide(a,b)):
def inner(a,b):
print("I am going to divide",a,"and",b)
if b == 0:
print("Whoops! cannot divide")
return
return func(a,b)
return inner
But this would cause divide(a,b) to be executed right in the top line
in the new example 'a_stand_alone_function' did not have () on the end. Which means it was treated as an object.
So my idea of it looking like this def smart_divide(divide(a,b)):
doesn't make sense because the function won't be treated as an object anymore
This leaves me confused as to how smart_devide
get the information passed as a parameter.