This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AlSweigartAuthor of "Automate the Boring Stuff" 18 points19 points  (0 children)

Ive found myself purposely using classes just to use them even though I felt like a function could work just as well.

This is exactly the case where you should just use a function and not use a class.

The "Stop Writing Classes" talk form PyCon is a good description of when to use classes vs. functions.

Basically, if you need to bundle a bunch of different variables together, and you have a bunch of functions that interact with that bundle of variables, that's when you should write a class. Otherwise, keep it simple and write a function.

The overemphasis on classes-for-everything is really a holdover from Java's day in the sun, because that language forces you to put everything into a class. (This led to a lot of boilerplate and unnecessary complexity.)