Re:Python-patterns – A collection of design patterns/idioms ...
by roelschroeven on Tuesday May 31, 2022 @06:01AM
(#31567727)
You want something that encapsulates state and a method. That's precisely what classes are for! I feel going against how the language is meant to so is just going to make things more difficult for yourself.
Is using modules like that nicer? That's subjective, I guess. I don't think it's nicer. You could use dataclasses to make classes somewhat nicer (though that is subjective too).
It it leaner than classes? I don't think it's something to worry about. Maybe use __slots__ in your class if and where needed. Is an encapsulated module still leaner than a class?
In the end both modules and classes are namespaces. Modules are singletons, classes have as many instances as you need.
Is using modules like that nicer? That's subjective, I guess. I don't think it's nicer. You could use dataclasses to make classes somewhat nicer (though that is subjective too).
It it leaner than classes? I don't think it's something to worry about. Maybe use __slots__ in your class if and where needed. Is an encapsulated module still leaner than a class?
In the end both modules and classes are namespaces. Modules are singletons, classes have as many instances as you need.