Difference: abstract class and interface in Python

+1 vote
How is abstract class different from interface in Python?
Oct 30, 2018 in Python by findingbugs
• 3,140 points
1,178 views

2 answers to this question.

0 votes

An interface, for an object, is a set of methods and attributes on that object.

In Python, we can use an abstract base class to define and enforce an interface.

For example, say we want to use one of the abstract base classes from the collections module:

import collections
class MySet(collections.Set):
    pass

If we try to use it, we get an TypeError because the class we created does not support the expected behavior of sets:

class ListBasedSet(collections.Set):
    """Alternate set implementation favoring space over speed
    and not requiring the set elements to be hashable. 
    """
    def __init__(self, iterable):
        self.elements = lst = []
        for value in iterable:
            if value not in lst:
                lst.append(value)
    def __iter__(self):
        return iter(self.elements)
    def __contains__(self, value):
        return value in self.elements
    def __len__(self):
        return len(self.elements)

s1 = ListBasedSet('abcdef')
s2 = ListBasedSet('defghi')
overlap = s1 & s2

There are other things we could do with this, like register virtual subclasses that already implement these interfaces, but I think that is beyond the scope of this question. The other methods demonstrated here would have to adapt this method using the abc module to do so, however.

answered Oct 30, 2018 by Priyaj
• 56,520 points
+1 vote
If abstract class contains only abstract methods then it is interface.

abstract class can contain both abstract methods and concrete methods
answered Jun 21 by anonymous
I'm new to python and it would be helpful if you explained what concrete methods are. Thank you

Hi @Harsh, A concrete method means, the method has a complete definition. but it can be overridden in the inherited class.

Related Questions In Python

+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 56,520 points
213 views
+1 vote
1 answer

What's the difference between eval, exec, and compile in Python?

exec is not an expression: a statement ...READ MORE

answered Aug 28, 2018 in Python by Priyaj
• 56,520 points
496 views
0 votes
1 answer

Difference between module and Package in Python

A module is basically a single file ...READ MORE

answered Nov 13, 2018 in Python by SDeb
• 13,160 points
115 views
0 votes
1 answer

In Python, what is difference between Array and List?

Lists and arrays are used in Python ...READ MORE

answered Mar 15 in Python by Taj
• 1,040 points

edited Mar 18 by Omkar 2,764 views
0 votes
1 answer

Difference between a normal def defined function and lambda function in python?

Have a look at this code: # Python ...READ MORE

answered May 20 in Python by Trisha
43 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 222 views