Ask HN: What do you think about using classes in Python?
The team I'm working with use python for backend scripts and now they want to change all the python code to use Classes. I don't think this is necessary because we'll end having a lot of unwanted boilerplate code. I think classes are suited if you want many instances of the same thing, each with its own isolated state.. otherwise it's useless. You can have a clean and concise Python code without classes.
How would you handle that? What would be your arguments?
19 comments
[ 4.4 ms ] story [ 62.8 ms ] threadWhy, doesn't it work?
Always potentially a bad idea, regardless of what you're changing to.
You learn a lot and hate programming...
Unless the codebase is awesome.
Ha!
difficult to comment properly without more context.
Pros: enforced encapsulation, things mix and match easily, inheritance (sometimes useful)
Cons: over-organization/God objects/anti-patterns (especially of the inheritance variety), bad garbage collection patterns, classes that are "only behavior and not state" [1])
(Also, if it ain't broke, don't rewrite it. Not unless their programming concerns have been conveniently redacted from your post.)
[1]: http://eev.ee/blog/2013/03/03/the-controller-pattern-is-awfu...
Expanding loses important context.
> Pros: enforced encapsulation, things mix and match easily, inheritance (sometimes useful)
As an example of the point above about context, Python -- and this is common among dynamic OO languages as opposed to C++/Java-style static class-based OO languages -- has encapsulation by convention rather than enforcement.
> Cons: over-organization/God objects/anti-patterns (especially of the inheritance variety), bad garbage collection patterns, classes that are "only behavior and not state" [1])
I don't think any of these are cons of OOP. All but the last are doing OOP badly -- but any paradigm can be done badly.
The last is not OOP, though its something that can be done in most OO languages. OTOH, there's no real reason to oppose it other than OO fundamentalism -- an object or class that contains only behavior and not state may not be really an example of OOP, but its not a "Con" of OOP, its just a manner in which modules as found in many procedural/functional languages can be implemented in some languages where OOP is the paradigm from which the primary syntactic structures are drawn.
* If your python code is organized in such a way that your classes will end up having no instance vars or methods, pragmatic advantages to using classes are limited. In other words, if you're switching to OOP, make use of that paradigm or it will indeed feel like mere boilerplate.
Are you sure you are not adopting Java or C++ conventions?
Python supports both OO and functional paradigms. For any project you would use the combination that is optimal for the project. Forcing the use of one or the other without considering the overall impact is unwise.
Let me guess what's happening here. Some kid recently read the gang of four design patterns book and now thinks he can turn your battle tested scripts into reusable extensible modules?
Quoting: "My own feeling is that object-oriented programming is a useful technique in some cases, but it isn't something that has to pervade every program you write. You should be able to define new types, but you shouldn't have to express every program as the definition of new types."
Show them this.
I'm still grateful for the moment I grew dissatisfied with OOP, or at least OOP in the Java/C# way. I was almost getting to an intermediate level in python (by myself) and had been thinking about a video game architecture problem I just couldn't solve.
I was still "indoctrinated" in the gang-of-four patterns and inheritance. When I discovered the solution, Entity Component Architectures[1], I also discovered the "composition over inheritance" philosophy. My mind was blown! Coupled with some Python's characteristics (duck-typing, everything is public, first-class functions) I was having a blast. Then I saw the video above, and all fell into place. Class based OOP have specific uses, but It's a shame how heavily enforced the paradigm is.
Now, for scripts or not-to-big programs in Python, I mainly use NamedTuples for passing data around and (mostly) pure functions.P.S. Another funny thing: At my job I use J2EE aka "lots of hinheritance and classes and boilerplate". Sometimes I just have to go home and write some code in python or lua to "cleanse my palate". It helps me get motivated for my personal projects.
[1] https://en.wikipedia.org/wiki/Entity_component_system
YAGNI -> You Aren't Gonna Need It [1]
If you want to know more about about Python I recommend Raymond Hettinger's videos [2]
About ECA (which I presume is Entity Component Architecture) I have to say two things: 1) I've seen this pattern/concept be called many different things: Entity-Component, Game-Object Game-Component, Entity-Systems, etc. And at least two major ways of implementing it. 2) Like most design patterns, they exist to help you design program using languages that use heavy class-based OOP and static typing. In Python everything is dynamic and it uses duck typing, so can "draw some inspiration" from the pattern instead of implementing it by the book.
[1] https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it [2] http://pyvideo.org/speaker/138/raymond-hettinger