0

What i want to do is to generate from given images new ones.

I have created transformation functions:

def trans_func_1(image,param1,param2):
 #do some transformation
def trans_func_2(image,param1,param2):
 #do some transformation

All functions operates on image level.

Then I have a big function which is called generate_image function, which takes as arguments a path to the folder with images, parameters for the written above functions and applies transformation functions to the images.

My idea is to define transformation functions as module-level functions and for the big functions create a class, that will look this

class ImageGeneration:
def __init__(self,path,trans_param1,trans_param2):
 
 self.path = path
 self.trans_param1 = trans_param1
 self.trans_param2 = trans_param2
 ...
def image_generation(self):
 for image in self.path:
 
 for i in self.trans_param1:
 img = trans_func_1(image,i)
 write_img(img) 
 for i in self.trans_param2:
 img = trans_func_2(image,i)
 write_img(img)
 

My question is: is it a good practice to use module-level functions inside a class? Or is it better to define them as @staticmethod

balderman
24k8 gold badges39 silver badges60 bronze badges
asked Jan 16, 2022 at 16:45

1 Answer 1

3

For your case, it is completely fine to let the module-level function instead of adding all of them to the class as static.

The class is responsible for handling the parameters and retrieving a generated image. The transformations functions are specific to their own transformation, thus not being necessary to add them to the handler.

This also allows you to have much better modularity. Depending on how many transformations you want to create, you may have a 500 line class where only 40 lines are handled and the rest are specific transformations. Separating each transformation into its own file (or separating them by scopes) would be my preferred way to go.

answered Jan 16, 2022 at 17:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.