Understanding how does a decorator really work [duplicate]












1
















This question already has an answer here:




  • How to make a chain of function decorators?

    16 answers




I'm starting studying decorators and I already hit an obstacle. First here is my code.



 def deco (f):

def coucou():
print("this is function{}".format(f))
return f()
return coucou

@deco
def salut():
print("salut")


def hi():
return salut()


I will try to explain my problem as well as I can with my bad English. If I understand it this is how things should happen: I execute my hi() function which returns salut() and because salut is modified by the decorator coucou will be executed and coucou returns ....... salut(), what i mean is that I expect an infinite loop but that doesn't happen and I don't understand why. Can anyone explain practically how decorators work?










share|improve this question















marked as duplicate by Bazingaa, Noctis Skytower, Community Jan 19 at 1:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • realpython.com/primer-on-python-decorators This may help in understanding how decorators work

    – Pro Q
    Jan 19 at 0:20











  • You may also want to have a look at the documentation: PEP-318 and PEP-3129.

    – Ondrej K.
    Jan 19 at 0:29
















1
















This question already has an answer here:




  • How to make a chain of function decorators?

    16 answers




I'm starting studying decorators and I already hit an obstacle. First here is my code.



 def deco (f):

def coucou():
print("this is function{}".format(f))
return f()
return coucou

@deco
def salut():
print("salut")


def hi():
return salut()


I will try to explain my problem as well as I can with my bad English. If I understand it this is how things should happen: I execute my hi() function which returns salut() and because salut is modified by the decorator coucou will be executed and coucou returns ....... salut(), what i mean is that I expect an infinite loop but that doesn't happen and I don't understand why. Can anyone explain practically how decorators work?










share|improve this question















marked as duplicate by Bazingaa, Noctis Skytower, Community Jan 19 at 1:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • realpython.com/primer-on-python-decorators This may help in understanding how decorators work

    – Pro Q
    Jan 19 at 0:20











  • You may also want to have a look at the documentation: PEP-318 and PEP-3129.

    – Ondrej K.
    Jan 19 at 0:29














1












1








1









This question already has an answer here:




  • How to make a chain of function decorators?

    16 answers




I'm starting studying decorators and I already hit an obstacle. First here is my code.



 def deco (f):

def coucou():
print("this is function{}".format(f))
return f()
return coucou

@deco
def salut():
print("salut")


def hi():
return salut()


I will try to explain my problem as well as I can with my bad English. If I understand it this is how things should happen: I execute my hi() function which returns salut() and because salut is modified by the decorator coucou will be executed and coucou returns ....... salut(), what i mean is that I expect an infinite loop but that doesn't happen and I don't understand why. Can anyone explain practically how decorators work?










share|improve this question

















This question already has an answer here:




  • How to make a chain of function decorators?

    16 answers




I'm starting studying decorators and I already hit an obstacle. First here is my code.



 def deco (f):

def coucou():
print("this is function{}".format(f))
return f()
return coucou

@deco
def salut():
print("salut")


def hi():
return salut()


I will try to explain my problem as well as I can with my bad English. If I understand it this is how things should happen: I execute my hi() function which returns salut() and because salut is modified by the decorator coucou will be executed and coucou returns ....... salut(), what i mean is that I expect an infinite loop but that doesn't happen and I don't understand why. Can anyone explain practically how decorators work?





This question already has an answer here:




  • How to make a chain of function decorators?

    16 answers








python python-3.x decorator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 0:33









Ondrej K.

2,80361022




2,80361022










asked Jan 19 at 0:06









Newgate AceNewgate Ace

184




184




marked as duplicate by Bazingaa, Noctis Skytower, Community Jan 19 at 1:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Bazingaa, Noctis Skytower, Community Jan 19 at 1:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • realpython.com/primer-on-python-decorators This may help in understanding how decorators work

    – Pro Q
    Jan 19 at 0:20











  • You may also want to have a look at the documentation: PEP-318 and PEP-3129.

    – Ondrej K.
    Jan 19 at 0:29



















  • realpython.com/primer-on-python-decorators This may help in understanding how decorators work

    – Pro Q
    Jan 19 at 0:20











  • You may also want to have a look at the documentation: PEP-318 and PEP-3129.

    – Ondrej K.
    Jan 19 at 0:29

















realpython.com/primer-on-python-decorators This may help in understanding how decorators work

– Pro Q
Jan 19 at 0:20





realpython.com/primer-on-python-decorators This may help in understanding how decorators work

– Pro Q
Jan 19 at 0:20













You may also want to have a look at the documentation: PEP-318 and PEP-3129.

– Ondrej K.
Jan 19 at 0:29





You may also want to have a look at the documentation: PEP-318 and PEP-3129.

– Ondrej K.
Jan 19 at 0:29












1 Answer
1






active

oldest

votes


















2














The f in coucou is the undecorated (original) version of salut.






share|improve this answer
























  • yeah but why and how? again if i got it, the decorator modify the function in its definition and makes it point permanently to the function returned by the decorator, am i wrong?

    – Newgate Ace
    Jan 19 at 0:13






  • 1





    @NewgateAce it doesn't modify the original function. It creates a new function, and returns a new function. So the global name salut now refers to the function returned by the decorator, i.e. coucou, but coucou's local name f, which is a free variable closed over by coucou, still refers to the original function

    – juanpa.arrivillaga
    Jan 19 at 0:16













  • @NewgateAce in other words, there is nothing magical about the decorator syntax. It is equivalent to def my_function(): ...; my_func = deco(my_func)

    – juanpa.arrivillaga
    Jan 19 at 0:24











  • @juanpa.arrivillaga i see now, f = deco(f) makes it clearer for me. thanks for your reply.

    – Newgate Ace
    Jan 19 at 0:30











  • @juanpa.arrivillaga what im about to say might be stupid, but when i print "salut" and "deco(salut)", they dont have the same reference ...

    – Newgate Ace
    Jan 19 at 0:46


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The f in coucou is the undecorated (original) version of salut.






share|improve this answer
























  • yeah but why and how? again if i got it, the decorator modify the function in its definition and makes it point permanently to the function returned by the decorator, am i wrong?

    – Newgate Ace
    Jan 19 at 0:13






  • 1





    @NewgateAce it doesn't modify the original function. It creates a new function, and returns a new function. So the global name salut now refers to the function returned by the decorator, i.e. coucou, but coucou's local name f, which is a free variable closed over by coucou, still refers to the original function

    – juanpa.arrivillaga
    Jan 19 at 0:16













  • @NewgateAce in other words, there is nothing magical about the decorator syntax. It is equivalent to def my_function(): ...; my_func = deco(my_func)

    – juanpa.arrivillaga
    Jan 19 at 0:24











  • @juanpa.arrivillaga i see now, f = deco(f) makes it clearer for me. thanks for your reply.

    – Newgate Ace
    Jan 19 at 0:30











  • @juanpa.arrivillaga what im about to say might be stupid, but when i print "salut" and "deco(salut)", they dont have the same reference ...

    – Newgate Ace
    Jan 19 at 0:46
















2














The f in coucou is the undecorated (original) version of salut.






share|improve this answer
























  • yeah but why and how? again if i got it, the decorator modify the function in its definition and makes it point permanently to the function returned by the decorator, am i wrong?

    – Newgate Ace
    Jan 19 at 0:13






  • 1





    @NewgateAce it doesn't modify the original function. It creates a new function, and returns a new function. So the global name salut now refers to the function returned by the decorator, i.e. coucou, but coucou's local name f, which is a free variable closed over by coucou, still refers to the original function

    – juanpa.arrivillaga
    Jan 19 at 0:16













  • @NewgateAce in other words, there is nothing magical about the decorator syntax. It is equivalent to def my_function(): ...; my_func = deco(my_func)

    – juanpa.arrivillaga
    Jan 19 at 0:24











  • @juanpa.arrivillaga i see now, f = deco(f) makes it clearer for me. thanks for your reply.

    – Newgate Ace
    Jan 19 at 0:30











  • @juanpa.arrivillaga what im about to say might be stupid, but when i print "salut" and "deco(salut)", they dont have the same reference ...

    – Newgate Ace
    Jan 19 at 0:46














2












2








2







The f in coucou is the undecorated (original) version of salut.






share|improve this answer













The f in coucou is the undecorated (original) version of salut.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 19 at 0:10









Scott HunterScott Hunter

33.3k74071




33.3k74071













  • yeah but why and how? again if i got it, the decorator modify the function in its definition and makes it point permanently to the function returned by the decorator, am i wrong?

    – Newgate Ace
    Jan 19 at 0:13






  • 1





    @NewgateAce it doesn't modify the original function. It creates a new function, and returns a new function. So the global name salut now refers to the function returned by the decorator, i.e. coucou, but coucou's local name f, which is a free variable closed over by coucou, still refers to the original function

    – juanpa.arrivillaga
    Jan 19 at 0:16













  • @NewgateAce in other words, there is nothing magical about the decorator syntax. It is equivalent to def my_function(): ...; my_func = deco(my_func)

    – juanpa.arrivillaga
    Jan 19 at 0:24











  • @juanpa.arrivillaga i see now, f = deco(f) makes it clearer for me. thanks for your reply.

    – Newgate Ace
    Jan 19 at 0:30











  • @juanpa.arrivillaga what im about to say might be stupid, but when i print "salut" and "deco(salut)", they dont have the same reference ...

    – Newgate Ace
    Jan 19 at 0:46



















  • yeah but why and how? again if i got it, the decorator modify the function in its definition and makes it point permanently to the function returned by the decorator, am i wrong?

    – Newgate Ace
    Jan 19 at 0:13






  • 1





    @NewgateAce it doesn't modify the original function. It creates a new function, and returns a new function. So the global name salut now refers to the function returned by the decorator, i.e. coucou, but coucou's local name f, which is a free variable closed over by coucou, still refers to the original function

    – juanpa.arrivillaga
    Jan 19 at 0:16













  • @NewgateAce in other words, there is nothing magical about the decorator syntax. It is equivalent to def my_function(): ...; my_func = deco(my_func)

    – juanpa.arrivillaga
    Jan 19 at 0:24











  • @juanpa.arrivillaga i see now, f = deco(f) makes it clearer for me. thanks for your reply.

    – Newgate Ace
    Jan 19 at 0:30











  • @juanpa.arrivillaga what im about to say might be stupid, but when i print "salut" and "deco(salut)", they dont have the same reference ...

    – Newgate Ace
    Jan 19 at 0:46

















yeah but why and how? again if i got it, the decorator modify the function in its definition and makes it point permanently to the function returned by the decorator, am i wrong?

– Newgate Ace
Jan 19 at 0:13





yeah but why and how? again if i got it, the decorator modify the function in its definition and makes it point permanently to the function returned by the decorator, am i wrong?

– Newgate Ace
Jan 19 at 0:13




1




1





@NewgateAce it doesn't modify the original function. It creates a new function, and returns a new function. So the global name salut now refers to the function returned by the decorator, i.e. coucou, but coucou's local name f, which is a free variable closed over by coucou, still refers to the original function

– juanpa.arrivillaga
Jan 19 at 0:16







@NewgateAce it doesn't modify the original function. It creates a new function, and returns a new function. So the global name salut now refers to the function returned by the decorator, i.e. coucou, but coucou's local name f, which is a free variable closed over by coucou, still refers to the original function

– juanpa.arrivillaga
Jan 19 at 0:16















@NewgateAce in other words, there is nothing magical about the decorator syntax. It is equivalent to def my_function(): ...; my_func = deco(my_func)

– juanpa.arrivillaga
Jan 19 at 0:24





@NewgateAce in other words, there is nothing magical about the decorator syntax. It is equivalent to def my_function(): ...; my_func = deco(my_func)

– juanpa.arrivillaga
Jan 19 at 0:24













@juanpa.arrivillaga i see now, f = deco(f) makes it clearer for me. thanks for your reply.

– Newgate Ace
Jan 19 at 0:30





@juanpa.arrivillaga i see now, f = deco(f) makes it clearer for me. thanks for your reply.

– Newgate Ace
Jan 19 at 0:30













@juanpa.arrivillaga what im about to say might be stupid, but when i print "salut" and "deco(salut)", they dont have the same reference ...

– Newgate Ace
Jan 19 at 0:46





@juanpa.arrivillaga what im about to say might be stupid, but when i print "salut" and "deco(salut)", they dont have the same reference ...

– Newgate Ace
Jan 19 at 0:46



Popular posts from this blog

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage