Hacker News new | ask | show | jobs
by flohofwoe 1641 days ago
...or one could just use Python without classes, just functions. TBH I never quite understood why Python has the class keyword, it's a much better language without.
3 comments

I thought like you for several years. Then one day, I needed a custom type. You can use dicts (or lists, or namedtuples, I guess?), but it just ends up being cleaner and more idiomatic to define a class for the type, because you can define common methods for them.

The article mentions quaternions. If you make a quaternion type (class), you can define addition, multiplication, comparison, etc. for it (methods). If you represent a quaternion any other way, you can't say a * b. Or maybe you can, but I don't know how.

You can represent it as whatever and monkey patch it's __mul__. It's a bit complicated with standard library structures that may be implemented in C, though.
attrs really isnt about OO-style classes - it's specifically meant to provide struct-like declarative data containers, and these can help bridge the gap between the toolset that Python provides and functional (data-first) programming styles.
This is an obvious troll post but python is a pure OO language, everything is a an object with an associated class. Functions are just instances of FunctionType.