Are Python __init__ complex methods bad?

I have a program that I am writing in Python that does the following:

The user enters the name of the folder. Inside this folder are 8-15 .dat files with different extensions.

The program opens these data files, enters them into the SQL database, and then allows the user to select various changes made to the database. Then the database will be exported back to .dat files. There are about 5-10 different operations that can be performed.

What I planned during the design was to create a standard class for each group of files. The user will enter a folder name and an object with certain attributes (file names, file dictionary, file version (there are different versions), etc.). To determine these attributes, you must open several such files, read the file names, etc.

Should this action be performed in the __init__ method? Or should this action be performed on different instance methods that are called in the __init__ method? Or should these methods be located somewhere else and called only when the attribute is required elsewhere in the program?

I already wrote this program in Java. And I had a constructor that called other methods in the class to set the attributes of the object. But I was wondering what the standard practice in Python is.

+6
source share
4 answers

Well, in Python there is nothing special about good OOP methods. Decomposing one large method into a bunch of small ones is a great idea in both Java and Python. Among other things, small methods give you the ability to write different constructors:

 class GroupDescriptor(object): def __init__(self, file_dictionary): self.file_dict = file_dictionary self.load_something(self.file_dict['file_with_some_info']) @classmethod def from_filelist(cls, list_of_files): file_dict = cls.get_file_dict(list_of_files) return cls(file_dict) @classmethod def from_dirpath(cls, directory_path): files = self.list_dir(directory_path) return cls.from_filelist(files) 

Also, I don't know how this happens in Java, but in Python you don't have to worry about exceptions in the constructor, because they are handled exactly. Therefore, it is perfectly normal to work with objects with similar exceptions, such as files.

+3
source

It looks like the action you describe by initialization so it would be nice to put them in __init__ . On the other hand, these actions look quite expensive and are probably useful in another part of the program, so you can encapsulate them in some separate function.

+1
source

The __init__ method is called when an object is created.

Based on the background of C ++, I believe that it is not good to do the actual work, other than initialization in the constructor.

+1
source

There is no problem with having a long __init__ method, but I would have avoided it simply because it was harder to test. My approach would be to create smaller methods that are called from __init__ . This way you can test them and initialize separately.

Whether they should be called as needed or in front of the front really depends on what you need to do. If they are expensive operations and, as a rule, not all of them are needed, then it might be better to call them when necessary. On the other hand, you may need to run them in front so that there is no lag when attributes are required.

From your question it is not clear if you really need a class. I have no experience with Java, but I understand that everything in it is a class. In python, it's perfectly acceptable to just have a function, if that's all it takes, and only create classes when you need instances and other cool things.

+1
source

Source: https://habr.com/ru/post/910514/


All Articles