Ask HN: What do you think about using classes in Python?
11–20 of 20 posts
Re: Ask HN: What do you think about using classes in Python?
#12> they want to change all the python code Why, doesn't it work? Always potentially a bad idea, regardless of what you're changing to.
Re: Ask HN: What do you think about using classes in Python?
#13if they mean "turn the currently fat, repetitive scripts into slim wrappers which call into a well-structured collection of well-composed classes of objects, which closely match the problem domain, thus promoting code reuse, and fix once, benefit everywhere behavior", then good. difficult to comment properly without more context.
Re: Ask HN: What do you think about using classes in Python?
#14It's easier to write bad OOP than bad procedural code. 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?
Re: Ask HN: What do you think about using classes in Python?
#15Quoting: "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."
Re: Ask HN: What do you think about using classes in Python?
#16(I don't think this is a strictly Python-based argument, so I'll expand to all of OOP.) I agree with the sentiment that Object-Oriented Programming is only useful in certain cases. Personally, as long as there's enough encapsulation that components can be composed and utilized well, classes are not useful. Pros: enforced encapsulation, things mix and match easily, inheritance (sometimes useful) Cons: over-organizatio…
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.
Re: Ask HN: What do you think about using classes in Python?
#17I loved this essay Paul Graham wrote: http://paulgraham.com/noop.html 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."
Re: Ask HN: What do you think about using classes in Python?
#18Show 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.
“Sometimes, the elegant implementation is just a function. Not a method. Not a class. Not a framework. Just a function.” – John Carmack
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.
Re: Ask HN: What do you think about using classes in Python?
#19Stop writing classes - Jack Diederich - https://www.youtube.com/watch?v=o9pEzgHorH0 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 inheri…
Re: Ask HN: What do you think about using classes in Python?
#20Stop writing classes - Jack Diederich - https://www.youtube.com/watch?v=o9pEzgHorH0 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 inheri…
I saw this video and showed them.. lol. I partially agree with the game example shown in the video.. I found the final code quite hard to understand. But I think that's due to my lack of knowledge in all the native stuff Python can do. I like when he says that you don't have to plan too much ahead for the future.. I think this generate phantom requirements that doesn't contribute for the solution. Just change the cod…
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