Search code examples
pythonclassmethodsinterface

Implementing interfaces in Python?


To my knowledge, there is no explicit built-in feature of Python that allows to implement interfaces. Consider the following situation:

I have a project with multiple classes. And some of them use a specific method. This method is not functionally or logically coherent with any of them, and technically should never appear there, not to mention that it's the same method and it's definitely a bad practice to copy and paste it in all these classes. Now I could create a class, and have all these classes inherit from it, so they can use this method... but then again, these classes have nothing in common with each other, and it would be silly for them to have a common superclass. I could create a separate class with this method, pass its instance as an argument everywhere and then invoke the method as a member function, but that also looks like a dirty move, and I would really like to do this in the most elegant way possible.

I think it is not useful to paste all this code here to emphasize the problem, I will just use a simplified model that focuses on what I want:

class 1():
    def class1_specific_method (self):
        common_method() 
    def common_method()
        #some code
        return
class 2():
    def class2_specific_method (self):
        common_method() 
    def common_method()
        #some code
        return

The common_method works exactly the same, is necessary in both situations, but is not cohesive with any of these classes. Normally, if it was Java, I would use some static class, or just implement an interface for those classes. Any ideas on how to make it look cleaner and more logical?


Solution

  • I don't see the problem of using inheritance, python is not java. I mean, python has multiple inheritance and it useful for mixins whose functionality is similar to java's interfaces. For example if your inherit from a class Class0 you solve your problem as follows:

    class Class0(object):
        ...
    
    class Class1(Class0, CommonMethodsMixin):
        def class1_specific_method (self):
            common_method() 
    
    class Class2(Class0, CommonMethodsMixin):
        def class2_specific_method (self):
            common_method() 
    
    class CommonMethodsMixin(object):
        def common_method():
            ...
    

    Given that you do not have any Class0 I do not see the problem of simply inheriting from CommonMethodsMixin class.