Javatpoint Logo
Javatpoint Logo

collections.abc Module Python

Python provides a powerful module called collections that includes many useful data structures beyond the built-in types like lists and dictionaries. One particularly useful module in collections is collections.abc, which provides a set of abstract base classes for collections. In this article, we will explore the collections.ABC module in detail.

Introduction to Abstract Base Classes

An abstract base class (ABC) is a Python class that defines the basic interface for a group of related classes without providing a full implementation. Instead, an ABC provides abstract methods that any concrete subclass must implement. This approach allows multiple concrete classes to share the same interface and ensures they all implement the required methods.

The collection.abc module provides a set of ABCs for common collection types in Python, such as lists, dictionaries, sets, and more. These ABCs can be used as a type of hint to indicate that a particular parameter or return value is expected to be a collection of a specific type. Additionally, they can be used to check if a particular object is an instance of a collection type.

Iterable ABC

The Iterable ABC is the base class for all iterable types in Python, such as lists, tuples, sets, and more. It provides a single abstract method called __iter__() that any concrete subclass must implement. This method should return an iterator object to iterate over the collection elements.

Here's an example of how to use the Iterable ABC to define a function that takes an iterable as an argument:

In this code, we have defined a function called print_elements() that takes an iterable as an argument. We have used the Iterable ABC as the type of hint for the argument to indicate that any iterable object can be passed in.

Sized ABC

The Sized ABC is the base class for all sized collections in Python, such as lists, tuples, strings, and more. It provides a single abstract method called __len__() that any concrete subclass must implement. This method should return the number of elements in the collection.

Here's an example of how to use the Sized ABC to define a function that takes a sized collection as an argument:

In this code, we have defined a function called is_collection_empty() that takes a sized collection as an argument. We have used the Sized ABC as the type hint for the argument to indicate that any sized collection object can be passed in.

Container ABC

The Container ABC is the base class for all container types in Python, such as lists, tuples, sets, and more. It provides a single abstract method called __contains__() that any concrete subclass must implement. This method should return True if the collection contains the specified element and False otherwise.

Here's an example of how to use the Container ABC to define a function that checks if an element is present in a collection:

In this code, we have defined a function called is_element_present() that takes a container as the first argument and an element as the second argument. We have used Container ABC as the type of hint for the first argument to indicate that any container object can be passed in.

Hashable ABC

The Hashable ABC is the base class for all hashable types in Python. A hashable object can be used as a dictionary key or element in a set. It provides a single abstract method called __hash__() that any concrete subclass must implement. This method should return a hash value for the object.

Here's an example of how to use the Hashable ABC to define a function that checks if an object is hashable:

In this code, we have defined a function called is_object_hashable() that takes an object as an argument. We have used the Hashable ABC as the type hint for the argument to indicate that any hashable object can be passed in.

Sequence ABC

The Sequence ABC is the base class for all sequence types in Python, such as lists, tuples, and strings. It provides several abstract methods that must be implemented by any concrete subclass, including __, get item__(), __len__(), and index().

Here's an example of how to use the Sequence ABC to define a function that finds the first occurrence of an element in a sequence:

In this code, we have defined a function called find_first_occurrence() that takes a sequence as the first argument and an element as the second argument. We have used Sequence ABC as the type of hint for the first argument to indicate that any sequence object can be passed in.

Mapping ABC

The Mapping ABC is the base class for all mapping types in Python, such as dictionaries. It provides several abstract methods that any concrete subclass must implement, including __getitem__(), __len__(), __iter__(), and keys().

Here's an example of how to use the Mapping ABC to define a function that finds the value associated with a key in a mapping:

In this code, we have defined a function called get_value() that takes a mapping as the first argument and a key as the second argument. We have used the Mapping ABC as the type of hint for the first argument to indicate that any mapping object can be passed in.

Set ABC

The Set ABC is the base class for all set types in Python. It provides several abstract methods that must be implemented by any concrete subclass, including __contains__(), __iter__(), __len__(), add(), discard(), and clear().

In this code, we have defined a function called find_common_elements() that takes two sets as arguments. We have used the Set ABC as the type of hint for both arguments to indicate that any set object can be passed in.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA