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: 36)
Reply


Messages In This Thread
Class test : good way to split methods into several files - by paul18fr - Jan-29-2024, 11:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 6 335 7 hours ago
Last Post: ebn852_pan
  Class and methods Saida2024 2 253 May-13-2024, 04:04 AM
Last Post: deanhystad
  Good class design - with a Snake game as an example bear 1 1,914 Jan-24-2024, 08:36 AM
Last Post: annakenna
  [split] Class takes no arguments bily071 2 687 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  Structuring a large class: privite vs public methods 6hearts 3 1,161 May-05-2023, 10:06 AM
Last Post: Gribouillis
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,849 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 1,003 Feb-15-2023, 05:34 PM
Last Post: zsousa
Question Take user input and split files using 7z in python askfriends 2 1,164 Dec-11-2022, 07:39 PM
Last Post: snippsat
  access share attributed among several class methods drSlump 0 1,087 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 2,710 Oct-04-2021, 07:07 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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