Grok this function to level up your Python
Look at this code:
- def itemgetter(key):
- def get_item(dictionary):
- return dictionary[key]
- return get_item
If you'd like to level up your Python... this is a great function to understand.
I'm not even claiming you will write functions like this very often. (Though I do myself, sometimes.) But in order to understand what this function does, you have to grok a few Python pieces people normally don't grok.
Such as:
- The idea of functions being objects, just like any other Python object.
- The nature of a Python variable, and how you can seamlessly park a function object in them.
- Python's memory model. Or, why does Python never segfault?
- What "dynamic" means, and what it doesn't mean, in this phrase: "Python is a dynamic language".
And when you grok all these grokkables...
What happens?
By the way, you use it like this:
- codes = {"a": 7, "b": 3, "c": 2}
- get_a = itemgetter("a")
- print(get_a(codes))
What happens when you grok all these points is this:
You can't help but level up as a dev using Python...
Standing out from the other, un-grokking Python devs.
So get grokking.