0
Redesigning functions on the fly?
Hi. I was reading the decorators section when I found a section explaining that we could permanently change a function, like: print_text = decorator(print_text) Does this change permanently the function as on its code, or does this only happen locally like an anonymous function? Thank you for your time. Have a nice day. :)
4 Respuestas
+ 2
the print_text function is changed permanently.
in fact look at the expression, is an assignement, so the left member change value according to the right member. In this case, print_text become the return value of decorators (arguments).
hope that makes sense for you :)
+ 2
i don't know java, but if you can reassign a function you can do that with a wrapper(don't look at the syntax):
myfunc (){
print ('func')
}
otherfunc (){
wrapper (){
print ('modified')
myfunc ()
}
return wrapper
}
now:
otherfunc ()()
otuput:
func
modified
so:
myfunc = otherfunc ()
it hink not many languages permit to reassing the identifier of functions
speakong of application, probably you'll see while working, i really reccomend you to read code written be other developers(for ex: code of third party packages)
for example it can be useful for modify the implementation of a function without altering too much the existing code or much better example, modify a Written-By-Other function code for your needs
+ 1
error
the output is:
modified
func
hope it helps :)
0
First, Thank you for the answer. Second, as a Java programmer redesigning a class or a method on the fly is not something I ever heard of. I mean, there is method overriding, anonymous classes (and now lambda), etc, but the original stays the same and it can be called and works as it was originally programmed.
So I can deduct that in python the methods (or functions in python) can be overriden and reassigned as the original, changing the function permanently. Am I correct? If yes, what are the pros of it? Could you give a concrete example?
Thank you for your time.
Have a nice day. :)