back

by eatonphil·11y ago·view on hn ↗
Responses seem to think this is Python 101. I think of it more as post-OOP Python. I have never seen this pattern before, so I was interested. Gave it a shot and it worked.

My question remains, why use Python objects vs these style objects? Sure you can say this doesn't have "support" for class-related functionality natively. But just like in Javascript, most of these can be implemented anyway.

Support inheritance by passing in the parent Function/Class and copying the attributes/values. Check the type by storing type name or check the function name via reflective programming.

My point is, I can see most of the functionality of Python classes replicated without the Python class syntax. So... why is there native class syntax when I can replicate most of the functionality with just functions.

At least from a devil's advocate perspective, why should I use classes? Is memory management better? That is the only topic I can imagine mattering if there is a library for creating objects from just functions per the example.

2 comments
It seems to me like pre-OOP Python. I'm not a fan of overusing classes, and a lot can be accomplished in Python without them. But from one perspective, you're just reimplementing (a subset of) classes and preferring to type out the underlying dictionary instead of letting the existing syntax do it for you. So one answer is because easier to type and read.

Another answer is that you are, in fact, using classes, you're just not using the 'class' identifier and syntax (assuming you're library was fleshed out to include all the missing features that your toy example is missing). Why doesn't Python offer three different class implementations and syntax? Why did you use a dictionary in your example, instead of implementing your own key-value object with hashing functions? Why are you using Python at all, instead of writing directly machine code?

Memory wise it's certainly much better in classes than in a naive implementation via these. Every time you create a new pseudo-class you'll be attaching all methods over and over again, eating up way more memory than the shared methods between python classes. You can get around this by defining the functions outside the class definition itself, but at that point it feels like it's defeating the purpose.