3

Basically, why do I see a lot of people saying that we shouldn't use Python classes? I'm still a novice Python programmer and to me classes actually look nice. I feel the answer to this question lies in a comprehension of the language that I yet don't own.

Being as straightforward as I can: is it, or is it not, good to write Python classes and to use OOP in Python?

halfer
  • 19,824
  • 17
  • 99
  • 186
RGS
  • 964
  • 2
  • 10
  • 27
  • 9
    Where on Earth are you seeing lots of people saying this? – lanzz Mar 02 '14 at 23:50
  • For example, here on stackoverflow! – RGS Mar 02 '14 at 23:53
  • @RSerrao: Can you give examples of any questions/answers where people said that? – BrenBarn Mar 02 '14 at 23:55
  • 1
    @lanzz As always, it depends and only applies to specific cases, but see for example the [PyCon talk "Stop Writing Classes"](http://pyvideo.org/video/880/stop-writing-classes). –  Mar 02 '14 at 23:56
  • 1
    Indeed. http://stackoverflow.com/questions/2573135/python-progression-path-from-apprentice-to-guru 1st answer, 9th bullet point – RGS Mar 02 '14 at 23:57
  • 1
    @RSerrao His logic is flawed. Functional programming and object oriented programming are orthogonal concepts - they don't interfere with each other, they can coexist. Just look at Scala. – dcastro Mar 02 '14 at 23:59
  • 1
    @dcastro Look at who? – RGS Mar 03 '14 at 00:01
  • 2
    @dcastro and RSerrao: Note the sarcasm-indicating quotes around "better", and the final bullet point, "find balance". –  Mar 03 '14 at 00:02
  • 1
    @RSerrao [Scala](http://www.scala-lang.org/) is a 100% object oriented language focused on functional programming. Saying "don't use classes, use functional programming instead" makes no sense. – dcastro Mar 03 '14 at 00:03
  • 1
    @delnan Oh sh, I left my sarcasm detector at home :'( – dcastro Mar 03 '14 at 00:05
  • @delnan So, this means that I misunderstood it? – RGS Mar 03 '14 at 00:05
  • @RSerrao You misunderstood that one answer, yes. But I do think that there is some backlash in the Python community against classes used for certain purposes. I know that I occasionally tell people they don't need a class for the thing they're doing. –  Mar 03 '14 at 00:07
  • 1
    "Don't use variables, use operators instead" or "`dicts` are better than `lists`" both make as much sense as this. – Joel Cornett Mar 03 '14 at 00:08

1 Answers1

6

There are a few ways we can interpret this. I see two.

  1. The idea to not use classes isn't a rejection of OOP in general. It's the acknowledgement it's a tool to be used when appropriate. Because Python isn't strictly object oriented, you can just use a function when a function is appropriate. You might be hearing people reject classes because they don't offer any advantage for the project and result in bloated code. This is the concept behind the Stop Writing Classes talk.

  2. Much to Guido's dismay, Python can be used for functional programming as well. Maybe the people you're talking to are interested in functional programming and avoiding state. Instead they want to focus on data and its transformations.

munk
  • 12,340
  • 8
  • 51
  • 71
  • 6
    +1. I confess that when I see former Java programmers wrapping everything in classes, it makes me itch. – Joel Cornett Mar 03 '14 at 00:09
  • 1
    RE first point, I think the point is not "they're too heavy weight for the project" but rather "they offer zero advantage (but add some bloat) over a function solving the same task". –  Mar 03 '14 at 00:11
  • 1
    @delnan I was referring to code bloat, not performance characteristics. I like your wording. – munk Mar 03 '14 at 00:12
  • 1
    I didn't think "heavy weight" referred to performance specifically, but I wasn't sure what it *did* refer to, so here we are :-) Note that "bloat"/"heavy weight" may also be taken to mean "more concepts" and "namespace pollution", both of which are... arguably but somewhat widely accepted. –  Mar 03 '14 at 00:14
  • 3
    I confess that when I see a project full of `def` functional programming, it makes me itch. @JoelCornett – John Mee Mar 03 '14 at 00:17
  • 4
    @JohnMee Oh jeez, if only there was some sort of middle way. If only one could mix and match paradigms ... oh listen to me talking nonsense. Back to the torches and pitchforks, folks! –  Mar 03 '14 at 00:24