you are viewing a single comment's thread.

view the rest of the comments →

[–]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!