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. :)

10th Nov 2016, 11:31 PM
João Palrinhas
João Palrinhas - avatar
4 Réponses
+ 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 :)
11th Nov 2016, 2:58 PM
Wombato Wom
Wombato Wom - avatar
+ 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
13th Nov 2016, 1:30 PM
Wombato Wom
Wombato Wom - avatar
+ 1
error the output is: modified func hope it helps :)
13th Nov 2016, 1:32 PM
Wombato Wom
Wombato Wom - avatar
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. :)
11th Nov 2016, 4:44 PM
João Palrinhas
João Palrinhas - avatar