3 ms·
Modifiability is also true of python objects and classes. This is basically reinventing obj.__dict__ The real difference is that since it's not an object, "sel
by fluidcruft 3y ago
Modifiability is also true of python objects and classes. This is basically reinventing obj.__dict__
The real difference is that since it's not an object, "self" isn't in the function signature which to be fair can be annoying for a collection of stateless functions. So this is actually closer to a module.
- sanderjd 3y agoYeah, you alluded to this at the end, but you don't need to define these as methods on an instance (requiring `self`) if they don't require state.
- fluidcruft 3y agoTo be fair, I have come prefer approaching the problem stated in the link in the exact opposite direction. Specifically the part about having a collection of functions with signatures that are similar/identical. The example in the post is of a simple signature, but if you have more complex (and long) signatures and a bunch of functions that use them, it might be worth thinking about replacing the argument list with a frozen dataclass. @dataclass(frozen=True) class MyArguments: a: int, b: str, c: list = None def func1(self): ... def func2(self): ... etc. You can also sprinkle in @property and functools things like @cached, @cached_property when desired. It also lets you create functions that transform MyArguments into different MyArguments.
- sanderjd 3y agoYeah I'm with you here too.
- fluidcruft 3y agoThe struggle to figure out what the hell to name MyArguments is real. But that's also true about picking a name for the dict-of-functions-that-have-a-similar-weird-signature. :D