Is OOP Pythonic?
I am a strong believer in procedural programming, with some functional goodness thrown in. My argument is that procedures and functions complement each other well. Procedures change state, while functions produce new state. Dennis Ritchie figured this out almost 50 years ago and granted us C. People loved and still love C for it's simplicity and clear separation of data and procedures. You were granted a way to introduce your own structured data types, and that simple but powerful feature was enough for all kinds of complex data representations.
So what all this has to do with Python? Well, Python has a builtin way of implementing structures, but because of it's dynamic nature, that representation in the end boils down to simple key-value pairs -> dictionaries. So, why Python programmers prefer making classes when the language has dictionaries? Not only is using builtin types faster, it should also be more Pythonic, since you are exploiting one of the core features of Python: it's dynamic nature. Just look at LOAD_ATTR opcode, it's insane how slow that thing is.
If it quacks like a duck...or better yet, if it has a key, it is easy to check. So why not use it more?
6 comments
[ 5.6 ms ] story [ 27.7 ms ] threadBound methods. And yes, if you don't drive it into the ground, inheritance can be useful, also.
My argument goes against OOP, so I would just ignore inheritance.
Why have function parameters and local variables? Everything can be a dictionary. There can be an args and locals which are built-in sybmols. When a function executes, you access keys in args to get it arguments, and keys in locals to work with local variables. For more nuance, there can be fixargs which is a vector and keyargs which is a dict. If you want a new local variable, you just insert a new key into locals.
API design would be tricky to do without classes, but what is stopping us from making typed dictionaries? If I'm not mistaken, something similar already exists in stdlib.
Anyway, my intention was not to argue future design of Python. I just believe that writting classes complicates the overal program design, leading to unpythonic code.