It's most useful when you're using some data you already have as the dictionary key. A usecase i had for this was a binary file parser with 10 types of event markers. It was originally coded with if/elif, but performance was a pretty big consideration. Using the event markers as keys to a dictionary dispatch improved performance by about 15% and made the code significantly more readable.
Match only just came out in Python 3.10, so it doesn't universally work. I think you implied that in your comment, so I wanted to add context for others.
This is fun to play around and basically what Python does under the hood to implement classes. In Python2 it was even more obvious that classes are just fancy wrappers around a dict called, unsurprisingly, __dict__.
Note: this won't work in Python3 because the class.__dict__ becomes immutable at some point after declaring it, but the attribute name resolution stays the same. And it gets more interesting once you throw inheritance into the mix.
My favorite way to implement this is with decorators. I used this to make a dispatch table for reading objects from a MySQL database.
(Yes I know I should be using UserDict below. You should too and don't subclass dict like I did.)
class FuncRegistry(dict):
"""Creates a registry of hashable objects to function mappings."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def register_for(self, key: Hashable) -> Callable:
"""Decorator to register functions in the registry.
Parameters
key: Hashable
The key which should point to this function
Returns: Callable
Returns a decorator that registers the function to the key"""
def decorator(fn: Callable) -> Callable:
self[key] = fn
return fn
return decorator
qreg = FuncRegistry()
@qreg.register_for('foobr')
def handle_foobr(arg1, arg2):
# do something here then
return
qreg['foobr']('ooo its an arg', 'oh look another arg')
10 Comments
sem@lemmy.ml · 5 pts · 2y
To be honest, for me it looks as very strange pattern.. If one wants to have static container with functions, there is Enums or classes.
Zeth0s@lemmy.world · 2 pts · 2y
With classes you needs getattr in python to achieve the same. This is cleaner for simple mappings
Walnut356@programming.dev · 2 pts · 2y
It's most useful when you're using some data you already have as the dictionary key. A usecase i had for this was a binary file parser with 10 types of event markers. It was originally coded with if/elif, but performance was a pretty big consideration. Using the event markers as keys to a dictionary dispatch improved performance by about 15% and made the code significantly more readable.
vox@sopuli.xyz · 4 pts · 2y
not unique to python.
function pointers and hashmaps exist in basically all languages
like in lua:
JavaScript:
Rust (using hashmaps with string keys is extremely inefficient tho, there are much better options here but whatever)
SubArcticTundra@lemmy.ml · 3 pts · 2y
I think a similar result might now be achievable with match statements
kiwifoxtrot@lemmy.world · 0 pts · 2y
Match only just came out in Python 3.10, so it doesn't universally work. I think you implied that in your comment, so I wanted to add context for others.
SubArcticTundra@lemmy.ml · 2 pts · 2y
Oh, yes. I'm excited for the arrival of match. I've used it in rust and it's very nifty and Pythonic
sirdorius@programming.dev · 2 pts · 2y
This is fun to play around and basically what Python does under the hood to implement classes. In Python2 it was even more obvious that classes are just fancy wrappers around a dict called, unsurprisingly,
__dict__.Note: this won't work in Python3 because the
class.__dict__becomes immutable at some point after declaring it, but the attribute name resolution stays the same. And it gets more interesting once you throw inheritance into the mix.zeusbottom@sh.itjust.works · 2 pts · 2y
My favorite way to implement this is with decorators. I used this to make a dispatch table for reading objects from a MySQL database.
(Yes I know I should be using UserDict below. You should too and don't subclass dict like I did.)
edit: formatting
DigitalWebSlinger@lemmy.world · 2 pts · 2y
Broadly, this is a simple version of the Strategy Pattern, which is incredibly useful for making flexible software.
In Python, the example given is basically the classic bodge attempt to emulate switch-case statements.
sirdorius@programming.dev · 1 pts · 2y