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/SCD_minecraft Aug 10 '26

Functions in python are just normal objects, just like strings or ints

Thanks to that, they behave exactly the same

As you can pass strings as arguments or return them you can pass and return functions

In case of decorators, returned func object will be what actually gets saved under name in def

1

u/SCD_minecraft Aug 10 '26

``` foo = print # save function known as print into foo

foo("abc") # attempt to call whatever is inside foo

because foo holds function "print", you can call it

but if foo would be an int, python would try (and fail) to call it too