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.
Python
Welcome to the Python community on the programming.dev Lemmy instance!
π Events
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django π¬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
π Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
π Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
β¨ Python Ecosystem:
π Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- PythΓΆrhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API
Feeds
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.
With classes you needs getattr in python to achieve the same. This is cleaner for simple mappings
not unique to python.
function pointers and hashmaps exist in basically all languages
like in lua:
local table = {
add = function(a, b) return a + b end
}
table["add"](4, 5)
JavaScript:
{
add: (a, b) => a + b
}
Rust (using hashmaps with string keys is extremely inefficient tho, there are much better options here but whatever)
let map = HashMap::from([
("add", |a, b| a + b),
]);
//...
map["add"](1, 3);
I think a similar result might now be achievable with match statements
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.
Oh, yes. I'm excited for the arrival of match. I've used it in rust and it's very nifty and Pythonic
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')
edit: formatting
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.
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__
.
class Foo:
def __init__(self):
self.__dict__["instance_method"] = lambda: "instance_method"
self.__dict__["shadowed_class_method"] = lambda: "shadowed_class_method_from_instance"
Foo.__dict__["class_method"] = lambda cls: "class_method"
Foo.__dict__["shadowed_class_method"] = lambda cls: "shadowed_class_method_from_class"
f = Foo()
f.__dict__["dynamic_instance_method"] = lambda: "dynamic_instance_method"
print f.instance_method()
print f.dynamic_instance_method()
print f.class_method()
print f.shadowed_class_method()
OUTPUT:
instance_method
dynamic_instance_method
class_method
shadowed_class_method_from_instance
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.