Last week I taught a course that covered Decorators in Python.
Many know how to use them, but few can write them.
These are tricky because nested functions make our brains hurt.
Here are some hints for grokking them.
1/
In short, decorators allow you to inject orthogonal behavior before or after a function is executed.
But my favorite decorator definition is related to the construction and will help you easily create them: A callable that takes a callable and returns a callable.
2/
What do I mean by "orthogonal"?
A function should do one thing. If you want to add caching or logging, it really isn't related to the function (and could be applied to multiple functions). It is "orthogonal" behavior.
3/
What is "callable that takes a callable and returns a callable"?
Remember this. It will make decorators easy. When we execute a function in Python we "call" it.
So, you could also say: A decorator is a function that takes a function and returns a function.
4/
(Although Python has other *callables* like methods, classes, lambdas, or instances with .__call__. You can implement decorators with these, but we will ignore them here.)
5/
The simplest decorator is one I call the *identity* decorator. It is a function that accepts a function and returns a function:
6/
We can decorate a function by redefining it or using Python's syntactic sugar: "@". These two snippets are equivalent:
7/
If your brain is fine with the identity decorator, let's just expand it and write the decorator like this. (Remember "a function that takes a function and returns a function".)
8/
When we decorate with this new code, the call to "add" actually calls "wrapper" which calls add ("func") when it executes.
The key point is that we can inject logic before "func" and after. (See blue and orange in the image.)
9/
To make a caching decorator, insert the logic to look for a prior answer in #before, and stick the result of the function in the cache in #after.
The final bit with @wraps(func) updates .__name__ and .__doc__ so that code completion works in editors and you can pull up documentation.
11/
If you enjoyed this, follow me for more Python insights.
I have a book, Intermediate Python Programming, covering decorators and other fun constructs like generators, comprehensions, and functional programming. It is 30% off today.
Let's explore the "any" and "all" built-in functions in Python.
A 🧵
First of all, I'll teach you how to fish in Python before giving you the fish. 🐟🎣
The built-in "help" function will give you documentation in Python. Make liberal use of it and reach out to it before ceding control to a search engine.
These are "aggregation" or "reducing" functions. They take a sequence and collapse it to a single value.
"any" returns if any value was truthy.
"all" returns if all values were truthy.
Functional programming like this can be great for minimizing lines of code. But it is also great for making your brain spin. Here is how I would initially write this (if I were fancy, I would use the Sieve of Eratosthenes):
Can we collapse this into fewer lines of code? Certainly, (the functional style already showed that) we can. One thing to realize is that lines 3-6 can be replaced with an any call:
I'm frequently asked: Is it easy to get a tech job?
Probably not. It will require a degree (or a lot of hard work). People who say otherwise are probably selling you something.
1/
I've taught thousands of people Python over the years. Some pick it up quickly, others really struggle. That is why Universities have "weeder" courses. To sift out the strugglers.
2/
Does that mean you can't self-learn? No, but it will take a lot more work than just sitting back and watching YouTube.
3/
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
-Kernighan's Law
🤯
When I teach programming, I emphasize that to me, the most important part is that the code is "readable."
You write code (once or twice), but it is read all the time. Optimize for what happens a lot.
Not:
😦 Brevity
😦 Cleverness
😦 Using all of the features