Python Forum
Decorators and return statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decorators and return statements
#1
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!

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)
Reply
#2
The effect of the decorator is exactly the same as if you had written
def Divide(a, b):
    print(a/b)

Divide = smartdivide(Divide)
So Divide is redefined as the value returned by smartdivide(Divide), that is to say the function errorcheck. Now when you call Divide(), errorcheck() is actually called and it simply returns the value returned by errorcheck(). The effect of returning nothing is only a shorthand for returning the None value. Returning None has no effect with regard to script termination. The script terminates here because there are no other statements.
Reply
#3
Ah thanks Gribouillis - that makes sense.

Thanks again

Jon
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Decorators @ annotation drcl 3 337 Feb-24-2024, 06:12 AM
Last Post: Gribouillis
  Does @ at sign used for tother than decorators? ggpython000 1 497 Oct-12-2023, 09:08 AM
Last Post: buran
  Variable Scopes Decorators oclmedyb 6 2,669 Jan-15-2021, 02:13 PM
Last Post: oclmedyb
  How to print cache from Decorators with Memoization OlgaM 2 2,016 Jan-29-2020, 05:06 PM
Last Post: OlgaM
  Python decorators. dodiko1d 0 6,959 Oct-13-2019, 10:23 AM
Last Post: dodiko1d
  Decorators yksingh1097 2 2,473 Aug-14-2018, 01:44 PM
Last Post: yksingh1097
  learning decorators Prince_Bhatia 6 3,240 Aug-13-2018, 02:28 PM
Last Post: Prince_Bhatia
  Multiple "return" statements or "result" variable? WolfWayfarer 7 7,674 Jul-25-2018, 07:51 AM
Last Post: perfringo
  decorators within decorators mp3909 6 4,195 Apr-02-2018, 09:47 AM
Last Post: hf8bm
  decorators mp3909 9 5,487 Mar-25-2018, 05:28 PM
Last Post: mp3909

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020