- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Abstract Base Classes for Containers
There are some abstract base classes for the Python containers. There are different abstract base classes. These classes are like Containers, Hashtable, Generator, Set, MutableSet, Awaitable etc.
To use this module, we should import it using −
Some Abstract Base Classes are there, which are very useful. These classes are used to develop different container classes. For an example we can create a container which has full set functionality. To make that, we can use the Set Abstract base class. We need to supply some methods in our class. These are __contains__(), __iter__() and the __len__()
import collections.abc class ListSet(collections.abc.Set): def __init__(self, iterable): self.elements = lst = list() for element in iterable: if element not in lst: lst.append(element) def __iter__(self): return iter(self.elements) def __contains__(self, value): return value in self.elements def __len__(self): return len(self.elements) set1 = ListSet('ABCDEF') set2 = ListSet('DEFGHI') intersect = set1 & set2 intersect_iter = iter(intersect) try: while True: print(next(intersect_iter)) except: pass
D E F
- Abstract Base Classes in Python (abc)
- Python Exception Base Classes
- Abstract Classes in Java
- Abstract Classes in C#
- Interpreter base classes in Python
- Abstract Classes in Dart Programming
- What are abstract classes in Java?
- Abstract Method and Classes in Java
- What are abstract classes in C#?
- How to create abstract classes in TypeScript?
- Pure Virtual Functions and Abstract Classes in C++
- Difference between Traits and Abstract Classes in Scala.
- Abstract vs Sealed Classes vs Class Members in C#
- What is the difference between interfaces and abstract classes in Java?
- Explain Inheritance vs Instantiation for Python classes.