lundi 6 juin 2022

Is a good pratice to use inheritance to divide methods?

I got a long class (600 lines) and deciding to refactor all these nasty code. So I thinking about dividing the methods into multiple class:

    class Uploader:
        def print(self):
            print(self.a)

        def upload(self):
            ...
        ...

    class Indexer:
        def load_index(self):
            ...
        ...

    class JsonManager(Indexer):
        def save_chunk(self):
            ...
        ...
    class MainObject(Uploader, JsonManager):
        def __init__(self, a):
            self.a = a
            ...

one issue is that methods will use instance attribute of MainObject that is not defined in parents (Eg. print method in Uploader, a is not defined in Uploader but in MainObject). Is this a good practice or an another way exists ?

Aucun commentaire:

Enregistrer un commentaire