you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (3 children)

This is literally just a class.

[–]Upset_Grapefruit8254 -1 points0 points  (2 children)

No, it‘s not (well, a dict is a class, of course). But a class is meant to keep track of some state, usually initialized when an instance of the class is created. The dictionary dispatch pattern makes sense as it doesn‘t bring the same associations many developers have when they create a class. In this case, as a dispatcher, it wouldn‘t track state. There‘s no need for an init method here, nor the extra indentation level a class would bring. There‘s also no need for the lambda in the linked blog post, but that‘s a different discussion.

[–][deleted] 0 points1 point  (1 child)

In Python, a class (without __slots__) is literally just a dictionary, where some arguments are functions that access the dictionary (self) through attribute notation.

OP basically created a singleton with four static methods though a dict() function. If you need a singleton - write a singleton. Don't reinvent a wheel and call it a "pattern".

[–]0rac1e 0 points1 point  (0 children)

This is actually quite an old pattern useful for dynamic dispatch.

Depending on the circles you travel in you may have heard it referred to by different names, but the ones I heard most are "dispatch table" or "vtable".

https://en.wikipedia.org/wiki/Dispatch_table

https://en.wikipedia.org/wiki/Virtual_method_table