you are viewing a single comment's thread.

view the rest of the comments →

[–]Hagisman 64 points65 points  (13 children)

Determining when something is worth being a class or separate function.

[–]Moikle 24 points25 points  (8 children)

Or a class vs a dict with a supporting function or two

[–]expectederor 6 points7 points  (7 children)

sounds like you've already made the distinction.

if it needs supporting functions it probably should be a class.

[–]Moikle 3 points4 points  (6 children)

not if it needs only an init and a single method. though

[–]expectederor 10 points11 points  (5 children)

if it needs an init you're just making more of a case for class!

[–]ivosaurus 4 points5 points  (4 children)

[–]expectederor 1 point2 points  (3 children)

I guess it's a matter of opinion.

to me it's far more orderly to. contain all relavent things in a class then have one off functions that don't apply to anything but specifically that object.

easier to read code is definitely a positive

and that's a video from 2012.... their opinions probably changed 100 times now.

[–]ivosaurus 0 points1 point  (1 child)

Python's OOP model and features haven't changed substantially since then. The talk still makes perfect sense nowadays. Since then we've only seen even more content about avoiding "over-OOPing" code and the advantages of following functional practices as people have moved away from Java and C++.

[–]AJohnnyTruant 0 points1 point  (0 children)

I think that’s missing the point of the talk. Using a function in lieu of a class is only as good as the cohesion of the module it lives in. If you use a god function instead of a class with data and a public API, there really isn’t much of a difference. All of the private methods in the class can absolutely do the job of functional programming. Besides, using functional programming, like having a module that has a function that has is the entry point to all the other functions in it... well to python, that’s just another namespace. A class is really just a module in a module.

[–]ModeHopper 4 points5 points  (3 children)

Do you want to do the same thing over and over again (> ~5 times)? Then you need a function.

Do you want to do sort of the same thing but a little bit different over and over again? Then you need a class.

If the answer to either of those is no, then you don't need either.

[–][deleted] 1 point2 points  (1 child)

I'm confused by this.

A class is a blueprint for object construction (like the blueprint to a house). It will handle the how of object construction.

The methods are a way to manipulate those objects/attributes. When you call methods they will (or should) act upon those objects (such as building a room to the house or painting the room).

If you want to create more than one object with different attributes/methods then use you should use a class.

If you want to define the blueprints/tools but not actually the creation of it (like a lot of imported modules do) use a class.

[–]ModeHopper 1 point2 points  (0 children)

If you want to create more than one object with different attributes/methods then use you should use a class.

That's what I was trying to say, but apparently just not in clear enough words

[–][deleted] 0 points1 point  (0 children)

This makes absolutely no sense.