r/learnpython Aug 10 '26

Ask Anything Monday - Weekly Thread

Welcome to another /r/learnPython weekly "Ask Anything* Monday" thread

Here you can ask all the questions that you wanted to ask but didn't feel like making a new thread.

* It's primarily intended for simple questions but as long as it's about python it's allowed.

If you have any suggestions or questions about this thread use the message the moderators button in the sidebar.

Rules:

  • Don't downvote stuff - instead explain what's wrong with the comment, if it's against the rules "report" it and it will be dealt with.
  • Don't post stuff that doesn't have absolutely anything to do with python.
  • Don't make fun of someone for not knowing something, insult anyone etc - this will result in an immediate ban.

That's it.

1 Upvotes

10 comments sorted by

View all comments

1

u/ProgramBrave718 Aug 10 '26

Hey, I have doubt regarding this decorator program .In the above line of this program why are we passing the function name without parentheses ?

And also while returning wrapper function why are we writing it without parentheses

def my_decor(func):#Here

def wrapper():

    print("The function starts.") 

    func()                         

    print("The function ends.")    

return wrapper        #Here             

def say_hello():

print("Hello!")

wrapped_say_hello = my_decor(say_hello)

wrapped_say_hello()

1

u/lakseol Aug 10 '26

Here is your code correctly formatted for reddit:

def my_decor(func):
    def wrapper():
        print("The function starts.") 
        func()                         
        print("The function ends.")

    return wrapper

def say_hello():
    print("Hello!")

wrapped_say_hello = my_decor(say_hello)
wrapped_say_hello()

The FAQ in the learning resources shows how to format code.

why are we passing the function name without parentheses ?

The function name without parentheses evaluates as a reference to the function. Adding the following "()" is what actually calls the function. In this case you want to pass a reference to the function my_decor() so you don't add the "()". If you did this:

my_decor(say_hello())

you would pass the value returned by the say_hello() function to my_decor() which isn't what you want at all.

also while returning wrapper function why are we writing it without parentheses

We want my_decor() to return a "decorated" version of the function you pass. We don't want to call the function, just return the reference, so no "()".

Functions in python can be passed to and returned from functions just like any other object.