+ 2

Is it possible to decorate a predefined function.

How could one decorate print() for example

1st Jul 2018, 11:00 PM
Oma Falk
Oma Falk - avatar
2 Answers
+ 3
Closest I found online seems to recommend (because "no, decoration happens at 'def' time") wrapping the functions and some other weirdness [link 1]. So...I took the method we've both recently used...overriding the builtin with a new name...then decorators work fine: https://code.sololearn.com/cvH27A0rt9hc/?ref=app ^^^ Here, the print() function auto-repeats itself twice and auto-clocks that duration (I include an arbitrary delay), using decorators, without modifying the print line itself. For ideas, I also looked at "tracers" (sys.settrace) -- try traceMe("print") at line 36: https://code.sololearn.com/cklgpC883uCT/?ref=app ...and late binding [link 2] "In Python, you can rebind any function..." [1] https://stackoverflow.com/questions/12476402/is-it-possible-to-apply-my-own-decorators-to-builtin-methods-in-python [2] https://stackoverflow.com/questions/11588289/python-late-binding-dynamically-put-locals-in-scope
2nd Jul 2018, 3:34 AM
Kirk Schafer
Kirk Schafer - avatar
2nd Jul 2018, 6:01 AM
æŽç«‹ćš