0

I need something to organize some utilty functions.
Variant 1. create module and write all functions in this module.
Variant 2. (since python has no staticclass) create class with only static methods in main module

In general, there is no big difference.Like :

var 1)

import functionsmodule
workers = functionmodule.get_all_workers(**kwargs)

var 2)

workers = FunctionClass.get_all_workers(**kwargs)

I like the second one though. Questions is : what is best way to do such organization ?

KotGaf
  • 609
  • 5
  • 6
  • Any reason _why_ you like the second one? – miradulo Jun 08 '16 at 11:24
  • There is **no** best way to do such organization, it depends on what you want to achieve. Generally speaking however, the first approach is more suitable for scattered functions with little inter-connectivity, whereas a **class** is reserved for cases where the methods are inter-connected and use each other. – j4hangir Jun 08 '16 at 11:32

1 Answers1

0

You could also do:

from functionsmodule import get_all_workers, some_other_method

workers = get_all_workers(**kwargs)

After a couple of months learning python, this is my preferred solution. It cleans up the code without the need to reference a class or module. I'd only recommend this if your utilities method names WONT clash with builtins or other functions

I'd usually organise these re-usable/utilities within a common package and implement something like:

from common.functions import get_all_workers, some_other_method

workers = get_all_workers(**kwargs)

EDIT: Based on your "get_all_workers" method name - I'd imagine this should go into some sort of persistence/worker class rather than a general utilities class

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65