3 ms·
I've found that lots of dictionary dispatch in Python ends up feeling a lot nicer as class attributes. You get nicer typing properties, autocomplete, don't need
by rtpg 3y ago
I've found that lots of dictionary dispatch in Python ends up feeling a lot nicer as class attributes. You get nicer typing properties, autocomplete, don't need to type quotation marks in many places, and you don't end up in the weird "use lambdas or define functions and then refer to those in the dictionary".
Of course if you're trying to do a registry-pattern style thing the constraints are different (though there I also think that you end up wanting to base your stuff on classes whose metaclass registers itself based on a string-y class attr or something...)
Though in the end it's super dependent on what kind of code you're trying to write. No rule is absolute, especially in this category of aesthetics.
- Izkata 3y agoIf you're hardcoding it sure, but at that point you should consider just using a module with bare functions. This pattern is for when the lookup value comes from somewhere else, and to use it with attributes requires a more awkward getattr() call, and it has a less obvious error message if the value isn't in the lookup.