jeudi 16 février 2023

Should utility functions used within a class method be part of the class or outside of the class in its own file or same file as the class? [closed]

Suppose you have a class in test.py

class test:
  def method1(self, arg1, arg2):
    # do some stuff and call utils_func(arg1, arg2)

utils_func is only used within method1. I am wondering which one of the following is more appropriate:

  1. Define utils_func as a method for test. I'm not sure if this makes much sense since it doesn't directly operate on any attribute of method1.

  2. Don't do #1, but define utils_func within test.py so that you don't have to import it.

#3) Do #2, but define utils_func in a separate file, e.g., utils.py and then import it in test.py.

I think this question is partly opinion-based, but I'm wondering if there's any standard way to do this for Python development, and what things I should consider when making a decision?

Aucun commentaire:

Enregistrer un commentaire