r/shittyprogramming • u/CVh655FDBcZ1l • Apr 10 '19
One Line of Code™
https://gist.github.com/WorryingWonton/ce8a3a74b0298f0b734fe4dd0a883a63
101
Upvotes
r/shittyprogramming • u/CVh655FDBcZ1l • Apr 10 '19
View all comments
Show parent comment
1
u/Athandreyal Apr 11 '19 edited Apr 11 '19
The most obvious case would probably be logging functions. You can avoid cluttering up the target function, have access to both its incoming arguments and outgoing return, and can return statements and still log what they are from the target's point of view(your not technically in the target's context, but close), rather than having to see it in its recipient context.
Lets say we've got our mini program:
=============== RESTART: decorators.py =============== 54
Arbitrarily short and stupid, but it'll serve the purpose.
Suppose we needed to know what functions were called, when they were called, and what arguments they were given. This would be useful in debugging the process, or simply tracking its progression.
To accomplish that though is going to make quite the mess of our tidy little program:
and we'll get:
Expressive, but look at what we've done to those short little functions now!
Not only are they much less clear than before, they also had to have their logging customised for each one.
Decorators are essentially wrappers to a function, which can let you modify what happens when the function is called, without having to mess with the insides of that function at all. You get to do stuff just before the function is called, just after it was called, and can mess with its arguments and/or results as desired.
So to implement our logging as above, we simply change the original short program to this:
and calling it will get us:
The same as before, but now we don't have to clutter the functions at all. plus, we need only one line before the call rather than two lines inside it. And we can return a statement directly rather than having to evaluate it to an expression, assign it, log it, and then return it.
In this example, it hardly saves much, 22 lines vs 26. Take it to a bigger scenario, say we had 100 functions that needed logging. The decorator adds 1 line per, so +100 lines. The other approach adds 2 or 3 lines, so +200 to +300 lines.
Had those functions been lambdas, its even more extreme! You lose some syntactic sugar(@ notation doesn't work with lambdas) and
func.__name__will not be the name anymore, rather it will be<lambda>, but you can still use the exact same decorator code. I added the named argument name to be able to match the above outputs exactly once again, at the cost of requiring customisation per decorated lambda.So if we wanted to log lambdas and still have the same control over them:
And now we are even shorter than before, just 16 lines total with logging, and still the same output as above.
Whats more, for recursive lambdas, you can't log their recursions without a decorator/wrapper, or abusing python's overly eager execution style to allow for it. Leveraging the eagerness of python permits such absurdity as:
That will allow you to at least log the incoming arguments, but in this case, using prints which have no return, you either cannot log the returns or you cannot have the results. To have both you'd need to make the recursive call for both logging and return, but the arrival doesn't know which is which, logs everything, and chaos ensues.
For a damned good write up on the useful things they can do, see here: https://www.python-course.eu/python3_decorators.php