Jan-29-2024, 11:06 AM
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
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:
- either I use subclass(es) as in Solution 2
- either I use a single class and I just define functions in an external file as in Solution 3
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