Python Forum
Class test : good way to split methods into several files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class test : good way to split methods into several files
#1
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:
  • 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

Attached Files

.zip   forum_class.zip (Size: 1.52 KB / Downloads: 31)
Reply
#2
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.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
I'm focussing in splitting several methods into several files, size is not relevant accordingly but strategy is.
Reply
#4
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.
Reply
#5
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 mm
After 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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Good class design - with a Snake game as an example bear 1 1,845 Jan-24-2024, 08:36 AM
Last Post: annakenna
  [split] Class takes no arguments bily071 2 649 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  Structuring a large class: privite vs public methods 6hearts 3 1,083 May-05-2023, 10:06 AM
Last Post: Gribouillis
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,680 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 974 Feb-15-2023, 05:34 PM
Last Post: zsousa
Question Take user input and split files using 7z in python askfriends 2 1,109 Dec-11-2022, 07:39 PM
Last Post: snippsat
  access share attributed among several class methods drSlump 0 1,070 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 2,633 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,143 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How do I split a dataset into test/train/validation according to a particular group? 69195Student 1 2,289 May-12-2021, 08:27 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020