Class test : good way to split methods into several files - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Class test : good way to split methods into several files (/thread-41515.html) |
Class test : good way to split methods into several files - paul18fr - Jan-29-2024 Hi, Based on the following link, I've been performing basic tests to find a way to split methods into one or more files; I've retained solutions that seem the simpliest ones for me, and:
I cannot say what is the best way or if there are limitations using one solution rather another one: any comment and advise? Thanks Paul # -*- coding: utf-8 -*- # https://www.qtrac.eu/pyclassmulti.html Solution = 3 if (Solution == 1): import _DataStore class DataStore(_DataStore.Mixin): def __init__(self): self._a = 1 self._b = 2 self._c = 3 def small_method(self): print(f"a = {self._a}") return self._a # "_DataStore.py" file content # class Mixin: # def big_method(self): # return self._b # def huge_method(self): # return self._c obj = DataStore() obj.big_method() obj.huge_method() obj.small_method() elif (Solution == 2): import externalsubclassfile_2 class Multiple(externalsubclassfile_2.subclass): def __init__(self, A, B, C: float): super().__init__(A, B) self._c = C def Add(self): self._d = (self._a + self._c) print(f"d = {self._d}") return self._d obj = Multiple(A = 1.5, B = 2., C = 10.01) D = obj.Add() print(f"D_bis = {D}") E = obj.Prod() print(f"E_bis = {E}") # "externalsubclassfile_2.py" file content # class subclass: # def __init__(self, A: float, B: float): # self._a = A # self._b = B # def Prod(self): # self._e = (self._a * self._b) # print(f"e = {self._e}") # return self._e elif (Solution == 3): import externalsubclassfile_3 class Multiple(): def __init__(self, A: float, B: float, C: float): self._a = A self._b = B self._c = C def Add(self): self._d = (self._a + self._c) print(f"d = {self._d}") return self._d def Prod(self): return externalsubclassfile_3.Prod(self) obj = Multiple(A = 1.5, B = 2., C = 10.01) D = obj.Add() print(f"D_bis = {D}") E = obj.Prod() print(f"E_bis = {E}") # "externalsubclassfile_3.py" file content # def Prod(self): # self._e = (self._a * self._b) # print(f"e = {self._e}") # return self._e RE: Class test : good way to split methods into several files - Gribouillis - Jan-29-2024 The problem with your example is that there is no real «big method» or «huge method», and if there were such methods, I think the best solution would start by extracting code from these large methods to create more methods having a normal size. When these methods are split into several methods, there are techniques to split the class by grouping together related methods in new classes, then aggregate instances of these new classes to the object. RE: Class test : good way to split methods into several files - paul18fr - Jan-29-2024 I'm focussing in splitting several methods into several files, size is not relevant accordingly but strategy is. RE: Class test : good way to split methods into several files - deanhystad - Jan-30-2024 The problem I have is that I don't buy into the premise that there is a need to split classes into multiple files. Well designed classes will be fairly compact because they have the characteristics of being well defined. They don't do a lot of different things, so they don't need a lot of methods. I would expect methods with a lot of code to break that code up into functions. These supporting functions could be moved into a separate file, but I don't see any problem with a large file that is logically composed. I would much rather have fewer large modules than many smaller modules that contain extra code for the sole purpose of making the modules smaller. I've never had a need for a large data storage module as I've never thought of data storage as being a class. Data storage should be an attribute of classes that you want to save and restore, not a class that saves and restores things. The main window of a GUI should not be a large module. A window is a thing that displays views, a view being a group of controls that are used to perform an action. Most GUI programs with a main window will have multiple views, each of which can be, should be, their own module. The main window should do very little other than manage what views are made available to the user. RE: Class test : good way to split methods into several files - Pedroski55 - Jan-30-2024 Not an expert, so maybe I don't understand the problem, but if I had lots of different functions I needed in various places, I would pack them in a module, say, mymodule.py Then, import your module: import mymodule as mmAfter that, all the functions in mm are available to you. You can edit them, add new functions, or delete old ones. x = mm.func1() y = mm.func2() RE: Class test : good way to split methods into several files - felixandrea - Jul-17-2024 Hi Paul, Here’s a brief overview of the three solutions for splitting methods into multiple files: Solution 1: Using Mixins Pros: Promotes code reuse, keeps methods grouped. Cons: Can get complex with many mixins. python import _DataStore class DataStore(_DataStore.Mixin): def __init__(self): self._a = 1 self._b = 2 self._c = 3 def small_method(self): print(f"a = {self._a}") return self._a _DataStore.py: python class Mixin: def big_method(self): return self._b def huge_method(self): return self._c Solution 2: Using Subclasses Pros: Clear class hierarchy, method overriding. Cons: Can lead to tightly coupled code. python import externalsubclassfile_2 class Multiple(externalsubclassfile_2.subclass): def __init__(self, A, B, C): super().__init__(A, B) self._c = C def Add(self): self._d = (self._a + self._c) print(f"d = {self._d}") return self._d externalsubclassfile_2.py: python Sao chép mã class subclass: def __init__(self, A, B): self._a = A self._b = B def Prod(self): self._e = (self._a * self._b) print(f"e = {self._e}") return self._e Solution 3: Using External Functions Pros: Simple classes, easy to test. Cons: Less cohesive classes. python import externalsubclassfile_3 class Multiple: def __init__(self, A, B, C): self._a = A self._b = B self._c = C def Add(self): self._d = (self._a + self._c) print(f"d = {self._d}") return self._d def Prod(self): return externalsubclassfile_3.Prod(self) externalsubclassfile_3.py: python def Prod(self): Link Removed self._e = (self._a * self._b) print(f"e = {self._e}") return self._e Conclusion Solution 1: Use for modular, reusable code. Solution 2: Use for clear class hierarchy. Solution 3: Use for simple, easy-to-test classes. Choose based on your project needs and complexity. |