Live data from Hacker News

Ask HN: What do you think about using classes in Python?

news.ycombinator.com

1–10 of 20 posts

Ask HN: What do you think about using classes in Python?

#1
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?

Re: Ask HN: What do you think about using classes in Python?

#4
if 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?

#5
(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-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...

Re: Ask HN: What do you think about using classes in Python?

#6
* Python offers more encapsulation without classes than say PHP if you use importing properly, so a codebase without classes is not a big deal (I prefer OOD though).

* 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.

Re: Ask HN: What do you think about using classes in Python?

#7
Boilerplate code for classes in Python? WTF?

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.

Re: Ask HN: What do you think about using classes in Python?

#8
post #4

if 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.

Context is the key to getting a better answer to your question. For any sufficiently large code base you would need to adopt some structure, e.g. modules and/or well designed functions, to ensure that the system is maintainable. Classes are just one to encapsulate state and behaviour so it doesn't leak all over the place.

Re: Ask HN: What do you think about using classes in Python?

#9
post #2

> 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.

Although I prefer class oriented programming in Python, I have worked on projects where functions were a better fit to the domain concepts.
Post reply on HN