1

i have a question with this lines of code :

def first():
    print('Hello From First')

    def wrapper():
        print('Hi From Second')

    return wrapper


first()
# a = first()
# a()

Outpute:

Hello From First

when i call first() the inside Function not print but if i uncomment that 2 lines the output change to this :

Hello From First
Hello From First
Hi From Second

i wonder why assign function to variable and call that variable change the outpue?

thanks

5
  • 2
    Note quite sure what you are asking. Are you wondering why 'Hi From Second' is not printed in the first example? If so, it's because the function wrapper() is never called. Simply defining a function does not make it run. In the second example, wrapper() is returned, saved to the variable a, and then called: a() runs the actual code and prints. Commented Apr 23, 2022 at 21:19
  • 1
    first returns the function wrapper; you assign that function to a then call it. Commented Apr 23, 2022 at 21:21
  • @Mark hi. if function Wrapper() is never called then why when we assign first() to variable a and then call a(), "HI from Second" was printed? Commented Apr 23, 2022 at 21:24
  • @Alex you returned the function wrapper as the return from the function. then you called it. its that simple. Commented Apr 23, 2022 at 21:25
  • when you sat a = the result. you set a to the definition of function wrapper(). then you invoke the function. at that point you are running wrapper (using the name a and that is printing out the message you are seeing. Commented Apr 23, 2022 at 21:27

2 Answers 2

2

In the first case, you just execute first() what causes the first print. It also return the function wrapper since this is the return value, but nothing's done with this value.

When you uncomment the last 2 lines:

  1. in a = first() you execute first() again what causes the addition print of Hello From First, but this time a also contains a pointer to the wrapper function.
  2. You execute a() what causes execution of wrapper function that prints Hi From Second. Hope it was clear.
Sign up to request clarification or add additional context in comments.

5 Comments

it was clear but i have one question. you wrote : It also return the function wrapper since this is the return value, but nothing's done with this value. but if run this code : print(first()) output this : Hello From First <function first.<locals>.wrapper at 0x0000022A27842D40> and no Hi From Second in outpute
@Alex Yes, because when you print(first()), 2 things happening: you print Hello From First as written in first(), but you also print the return value of first(), which is a pointer to wrapper, and this is the other string that you see. Hi From Second is not printed since you didn't execute wrapper(), you just printed the pointer to it.
but if i change the print inside wrapper function,to return , still not print the Hi From Second.
def first(): print('Hello From First') def wrapper(): return('Hi From Second') return wrapper print(first())
That is because first() is still returning a pointer to the wrapper function, and no execution of wrapper is made. If in addition to the last change you'll also make first() return wrapper() instead of wrapper, then the function will be executed and you'll see the Hi from second.
1

Because inside first() function you return a pointer to the wrapper() function, so when you do: a=first() the first() function is executed and "Hello From First" is printed. Than the function returns the pointer to the function wrapper() and a contains it. So when you do a() it actually executes wrapper()

Read more here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.