0

NOTE: This question is not directly related to decorators, but rather the concept of them. It is NOT related to these 2 questions:

  1. What is decorator with "@" above a function in Python? [duplicate] (7 answers)
  2. How do I make function decorators and chain them together? (19 answers)

MY QUESTION: I'm currently experimenting with inner functions to get a good grasp of python decorators.

With this code below, I am able to successfully print "sup" through the wrapper function, even though I didnt explicitly call it.

def my_decorator(test):
    
    def wrapper():
        print(test)
    return wrapper

hello = my_decorator("sup")
hello()

This however, doesnt work if I do any of the following

  1. remove return wrapper
  2. directly call my_decorator("sup") without passing it into the hello function

correspondingly, I have 2 questions:

  1. does return wrapper automatically call the wrapper function?
  2. What's the difference between using a reference function like hello, and directly calling the my_decorator function?
  • 1
    `hello` *is* `wrapper`. Because `wrapper` is the return value of calling `my_decorator("sup")`. Calling `hello()` is calling `wrapper()`. – deceze Nov 26 '22 at 08:35
  • 1
    You may benefit from taking a look at the [pythontutor visualization](https://pythontutor.com/render.html#code=def%20my_decorator%28test%29%3A%0A%20%20%20%20%0A%20%20%20%20def%20wrapper%28%29%3A%0A%20%20%20%20%20%20%20%20print%28test%29%0A%20%20%20%20return%20wrapper%0A%0Ahello%20%3D%20my_decorator%28%22sup%22%29%0Ahello%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) to see what is happening. – Dash Nov 26 '22 at 08:36
  • Welcome to Stack Overflow. Thanks for editing your question and trying to clarify it. However, instead of starting with saying your question is not a duplicate of the 2 decorator questions, you could avoid this by removing the word "decorator" from your code, which probably caused the confusion. Your question is a very basic question, that for sure has been answered on SO already, so I understand why others would close vote. – wovano Nov 26 '22 at 09:54
  • 1
    On-topic: you write "_even though I didnt explicitly call it_", but your code DOES explicitly call it in the last line (`hello()`). Your "decorator" function (which is **not** a decorator) returns a function, which you assign to a variable `hello` and then (explicitly) call. – wovano Nov 26 '22 at 09:54
  • Answers: 1. No. 2. calling `my_decorator` doesn't do anything ,it just returns the function; calling hello calls the (inner) function which was returned before – wovano Nov 26 '22 at 09:56
  • PS: The following would also work, without "_passing it into the hello function_": `my_decorator("sup")()` – wovano Nov 26 '22 at 10:03
  • 1
    It’s not exactly a decorator, but employs very similar patterns. If you understand decorators, which are explained in as much detail as you want in the duplicates, you understand this example here too. – deceze Nov 26 '22 at 10:10

0 Answers0