0

Here is part of my code:

class classA(Something):

    CONFIG={
        "opt_a": "opt_b"
    }

    def construct(self):
         Some Unique Code
         .
         .
         .
         .
         Some Common Code

class classB(Something):

    CONFIG={
        "opt_a": "opt_b"
    }

    def construct(self):
         Some Unique Code
         .
         .
         .
         .
         Some Common Code

The Some Common Code part is over 200 lines and it is exactly the same in both classA and classB. I have to create 5 or more such classes. This will result in a lot of duplication.

Update: The Some Common Code parts consists of a lot of loops and other functions that rely on data from Some Unique Code.

Update after Raphael's Solution: There are some common variables in classA and classB. These variables are used both inside the "common code" as well as the "unique code". Moving them from one class to another creates variable undefined errors.

Here is my code after updates:

class Something(Else):
      
      CONFIG = {
          "alpha": "beta"
      }

      def setup(self):
            // Some Code
            return self

      def common_method(self):

            for i in texts:
                  // Manipulates Boxes

class classA(Something):

    CONFIG={
        "opt_a": "opt_b"
    }

    def construct(self):
         texts = []

         texts.append('Apple')
         .
         .
         .
         .
         self.common_method()

class classB(Something):

    CONFIG={
        "opt_a": "opt_b"
    }

    def construct(self):
         texts = []

         texts.append('Mango')
         .
         .
         .
         .
         self.common_method()
      

I am new to Python so I don't know how I can avoid this duplication. Any help would be appreciated.

Thanks.

3
  • 2
    Is adding a method "common_method" to the Parent Class "Something" an option? You can then call "common_method" instead of writing "Some Common Code" Commented Nov 28, 2020 at 12:34
  • The "common code" mostly consists of for loops and other methods of the Something class. Will this be a problem? Commented Nov 28, 2020 at 12:37
  • not at all. I posted an example. Commented Nov 28, 2020 at 12:41

2 Answers 2

2

As an alternative to @Raphael answer, we may focus on "construct" method and come up with such a structure:

class Something:

    def common_method(self):
        """ """

    def construct(self):
        self.common_method()


class ClassA(Something):

    def construct(self):
        # A-specific code
         super().construct()


class ClassB(Something):

    def construct(self):
        # B-specific code
         super().construct()

, but it is hard to say what will be better without more details.

UPDATE: If you need to manipulate with common variables alongside different methods, then you need to set them as object attributes (or class attributes).

In python there is a special method - __init__ that is used to construct an object.

Here is an example for your case:

class Something(Else):

    def __init__(self):
        self.texts = []
        
    CONFIG = {
        "alpha": "beta"
    }

    def setup(self):
        # Some Code
        return self

    def common_method(self):

        # Manipulates Boxes
        for i in self.texts:
            pass


class classA(Something):

    CONFIG={
        "opt_a": "opt_b"
    }

    def construct(self):
        self.texts.append('Apple')
        self.common_method()


class classB(Something):

    CONFIG={
        "opt_a": "opt_b"
    }

    def construct(self):
        self.texts.append('Mango')
        self.common_method()
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @hidden. :) I have added an update to the question based on Raphael's answer. Will your method solve the undefined variable error?
@RealNoob, both suggested structures should work for you. Show us a simplified example of your code so we could understand where do you do mistakes.
I will try to create a simplified version and post it in the question details. :)
Hi @hidden, could you please take a look at the code now? Thanks.
Hi @hidden. I am getting an TypeError: __init__() got an unexpected keyword argument 'window_config' error with the above code.
|
2

Simply outsource "common code" to a common method of the parent class

Note: if CONFIG is also identical for your sub classes you can outsource this also to the parent class

class Something:

    def common_method(self):
        pass  # common stuff
    

class ClassA(Something):

    CONFIG={
        "opt_a": "opt_b"
    }

    def construct(self):
         self.common_method()
         ...
         self.common_method()


class ClassB(Something):

    CONFIG={
        "opt_a": "opt_b"
    }

    def construct(self):
         self.common_method()
         ...
         self.common_method()

Update: The error in your update comes from the variable "texts" which is not defined within the common method. One way to solve this is to make texts an instance variable.

class ParentClass:
    
    def __init__(self):
        self.texts = []
    
    def common_method(self):
        for text in self.texts:
            print(text)

class classA(ParentClass):

    def construct(self):
        self.texts.append('Apple')
        ...
        self.common_method()

3 Comments

Thanks Raphael. I am getting errors about undefined vaiables when I move variables from parent class to child or child to parent
@RealNoob its hard to tell what is going wrong without an example
Hi @Raphael, could you please take a look at the code now? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.