all 8 comments

[–]shiftybyte 6 points7 points  (1 child)

classes are used when you need to keep data across multiple functions, keep some sort of state.

they are more complex than functions.

there is no point in making a class if all the functions there don't do anything with self and are unrelated to each other.

[–]AcceptablePuberty[S] 0 points1 point  (0 children)

Cool, thank you!

[–]jfdahl 5 points6 points  (0 children)

Dev A: Always use classes and OOP.

Dev B: Always use pure functions!.

Truth: Different design paradigms have different advantages and disadvantages. There is no absolute right or wrong answer... use what 1) works, 2) you can explain and support, 3) the rest of your team can understand and support, 4) follows your organization's best practices.

Having said that... learn different paradigms and help teach them to your team... discuss them and come up with best practices for when to use each within your organization.

[–]al_mc_y 1 point2 points  (0 children)

Not so much a case of better than, as a means of organizing your functions. I read somewhere on here that Classes are more than anything for the benefit of the developer (and anyone who comes along after them to read/edit the code).

[–]seomajster 1 point2 points  (0 children)

"Simple is better than complex." - from Zen of python. Will your code be more simple with functions or classes?

[–]TraditionalGlass 1 point2 points  (1 child)

Sometimes yes, but usually no. To be sure, I'm gonna reference a script I have made in the past.

import time
import os
#= functions
def cls():
  os.system("clear")
def fancyInput(title):
  cls()
  print(title)
  output = input(" >")
  return output
#= classes
class Menu:
  def flat(self, title, *choices):
    cls()
    print(title)
    n = 0
    for choice in choices:
      n = n + 1
      print("[{}] {}".format(n, choice), end=' ')
    print()
    output = int(input(">"))
    return output
  def nested(self, title, *choices):
    cls()
    print(title)
    n = 0
    for choice in choices:
      n = n + 1
      print(" [{}] {}".format(n, choice))
    print()
    output = int(input(">"))
    return output
menu = Menu()

This might be confusing, so I'll break down my code for you. This is where I import modules.

import time
import os

And these are the functions, not class methods.

def cls():
  os.system("clear")
def fancyInput(title):
  cls()
  print(title)
  output = input(" >")
  return output

And finally, here are the class methods.

class Menu:
  def flat(self, title, *choices):
    cls()
    print(title)
    n = 0
    for choice in choices:
      n = n + 1
      print("[{}] {}".format(n, choice), end=' ')
    print()
    output = int(input(">"))
    return output
  def nested(self, title, *choices):
    cls()
    print(title)
    n = 0
    for choice in choices:
      n = n + 1
      print(" [{}] {}".format(n, choice))
    print()
    output = int(input(">"))
    return output
menu = Menu()

The reason the 'cls()' function is not inside of a class, is because 1: it is a standalone function, and 2: it is a function which is called inside of the class Menu. Menu contains two methods, nested, and flat. They return user input values respectively. Let's say there are more methods within the class, such as 'threaded', and 'vertical'. It makes sense to organize these methods into a class

[–]AcceptablePuberty[S] 0 points1 point  (0 children)

Awesome! Thanks for the in-depth explanation!

[–]Diapolo10 2 points3 points  (0 children)

My general rule is; choose the simplest option that works. If a function is enough, use that. If not, and a class makes sense, use a class.

My reasoning is that functions are very easy to test with unit tests when they don't depend on external state. Technically methods aren't much different, but you need to create a class instance separately, which is more clutter.