3 ms·
> Why do I really need a class, Among other cases, you need a class where natural, convenient, consumer-friendly use of a data structure should leverage featur
by dragonwriter 7y ago
> Why do I really need a class,
Among other cases, you need a class where natural, convenient, consumer-friendly use of a data structure should leverage features like operators and standard protocols that rely on the existence of particular methods (often special methods.)
> If I need a more performant data structure, I'm not writing it in Python in the first place.
While Python is linearly slow, that’s no excuse to use data structures with poor big-O performance (small n may be, but that's no more or less true of Python than of a more performant language.)
> I've seen some very experienced Pythonistas recommend using namedtuple instead of classes for most use cases
Namedtuple is just a function that returns a fresh class; you can't use “namedtuple instead of classes”, as it's a convenience mechanism for creating a class with certain commonly-useful features.
> Clojure programmers seem perfectly happy with just lists and maps
And vectors. And sets. And records. And a bunch of other common data structures.
But, yes, the use of multimethods for type-based dispatch that clojure uses replaces classes. But Python doesn't have multimethods built-in and the core language and stdlib don't use them (they are available in a non-standard library).
- kyllo 7y ago> you need a class where natural, convenient, consumer-friendly use of a data structure should leverage features like operators and standard protocols that rely on the existence of particular methods (often special methods.) I understand and I can't disagree, this just seems much, much narrower in scope than what classes are used for in typical Python code I see every day. Which is perhaps due to influence from other languages like Java/C++ etc.