Hi again,
I'm trying to understand Decorators and found this short script somewhere on the web. I wondered if anyone would be kind enough to walk through it with me as none of these 'return' statements are making sense. Maybe the problem is that I'm not sure how 'return' statements really work, and am confusing the whole thing with trying to use Decorators.
1. So, I assume that when the function Divide is called, it is passed to the function smartdivide, smartdivide inherits the parameters 'a' and 'b', and then passes 'a' and 'b' to the function errorcheck.
2. If the 'if loop' in errorcheck finds that 'b' equals zero, it prints the error statement, and then a return statement ends the script. Why does it do that? Does that return statement result in 'a' and 'b' = None? Or does returning nothing always end scripts wherever it appears?
3. If b != 0, errorcheck returns the function(a,b) Divide (now called 'received_function') to where? To smartdivide?
4. Is line 7 'return errorcheck' now passing the parameters of errorcheck to Divide, or passing received_function to Divide? Or neither?
Confused!
I'm trying to understand Decorators and found this short script somewhere on the web. I wondered if anyone would be kind enough to walk through it with me as none of these 'return' statements are making sense. Maybe the problem is that I'm not sure how 'return' statements really work, and am confusing the whole thing with trying to use Decorators.
1. So, I assume that when the function Divide is called, it is passed to the function smartdivide, smartdivide inherits the parameters 'a' and 'b', and then passes 'a' and 'b' to the function errorcheck.
2. If the 'if loop' in errorcheck finds that 'b' equals zero, it prints the error statement, and then a return statement ends the script. Why does it do that? Does that return statement result in 'a' and 'b' = None? Or does returning nothing always end scripts wherever it appears?
3. If b != 0, errorcheck returns the function(a,b) Divide (now called 'received_function') to where? To smartdivide?
4. Is line 7 'return errorcheck' now passing the parameters of errorcheck to Divide, or passing received_function to Divide? Or neither?
Confused!
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def smartdivide(received_function): def errorcheck(a,b): if b = = 0 : print ( "Error: Division by zero" ) return return received_function(a,b) return errorcheck @smartdivide def Divide(a,b): print (a / b) |