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,260 points
7,663 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
• 58,020 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, 2019 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

0 votes
1 answer

What is the difference between an interface and abstract class?

Interfaces An interface is a contract: The person writing ...READ MORE

answered Nov 30, 2020 in Python by Gitika
• 65,770 points
789 views
+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
• 58,020 points
2,394 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
• 58,020 points
5,062 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,300 points
2,397 views
+1 vote
4 answers

In Python, what is difference between Array and List?

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

answered Mar 15, 2019 in Python by Taj
• 1,080 points

edited Mar 18, 2019 by Omkar 152,789 views
0 votes
5 answers
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP