1

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?

2
  • 2
    Python uses Duck typing - if it quacks like a duck, it's a duck. Just implement the same method in all classes, no need to define an interface as such. If the code is identical in each case, make a free-standing function and have all the classes call it. Commented Aug 6, 2014 at 12:27
  • 1
    Although still hurts my object-oriented soul, the free standing function in a separate file looks the most reasonable. I guess I'm too much of a spoiled child of Java for this language ;) Commented Aug 6, 2014 at 12:31

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, technically yes. But as for the logic and maintainability, consider this. I have class dbutils, which manages database queries. And a class simulation, that does sth completely different - plotting. And they both use in several places a short method that converts array of Point objects to a list of X,Y coordinates. Should these methods inherit from one superclass just because of that? It's unnatural, creates confusion and propagates lies. I only meant that ;)
Where does inheritance come in? The method sounds like it is completely independent of either your dbutils or class simulation, and is just a separate library routine.
2

Why not just make it a top-level function instead? If you choose to just ignore all the things Python brings you over something like Java (modules, top-level/first-class functions, duck typing), of course the lack of interfaces will seem inconvenient.

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

5 Comments

Yes, that's what I did in the end, I was aware of that, just thought there is a cleaner way, not violating the OOP principles.
@MichałSzydłowski This doesn't violate OOP principles. common_method isn't related to either class; it's just used by both, and therefore should be defined separately.
Yes, I know, but it exists loosely, outside of any class, that's what bothered me. Like I said, I'm a Java child. But from now on I will know.
Java's insistence that everything belong to a class for the sake of syntax is the greater violation of OOP principles here.
True, now I'm used to it, but it sometimes occurs to me how silly and unnecessary objects I'm forced to create just for the sake of the methods I need.
1

An alternative to Interfaces is using Abstract Classes from the ABC module. Using it you can do:

from abc import ABCMeta class AbstractClass(object): __metaclass__ = abc.ABCMeta def CommonMethod(self): print "Common Method for all subclasses!" ... class MyClass(AbstractClass): pass >>> obj = MyClass() >>> obj.CommonMethod() Common Method for all subclasses! 

Now you have an abstract class from which your classes can inherit common behaviour. If you want to have multiple interfaces, you could use multiple inheritance with ABC.

However, multiple inheritance in Python should render useless the need for Interfaces in the first place (in theory, that is). In certain situations, ABCMeta can provide a mechanism similar to and Interface, but it also feels like you're stretching the language a bit.

TL;DR: There are no interfaces in Python. ABC's can provide a work arround.

Edit: There's also zope.interface, but I'm not familiar with its usage.

1 Comment

True, but in the end, it is logically incoherent to have this class above two completely unrelated classes. Thanks for another opinion, though, ultimately I think the lesser evil is just using a globally defined function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.