Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the Callable ABC in the collections ABC module?

Tags:

python

abc

The Python collections.abc module contains many handy ABCs for checking various features of objects, but one that doesn't appear to belong is Callable. No standard collection is callable, and PEP 3119 doesn't provide any reasoning or even mention the Callable ABC, so why is it in this package instead of somewhere else?

Context: I'm writing a Python->Java compiler for fun, and I just wanted to see if there was any reasoning behind the decision so I could list that reasoning in my code.

like image 532
Octavia Togami Avatar asked Apr 20 '15 23:04

Octavia Togami


People also ask

What is Python Collection ABC?

Source code: Lib/_collections_abc.py. This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whether it is a mapping. An issubclass() or isinstance() test for an interface works in one of three ways.

Why do we need ABC in Python?

The 'abc' module in the Python library provides the infrastructure for defining custom abstract base classes. Abstract class cannot be instantiated in python. An Abstract method can be call by its subclasses.

What is ABC module?

Python has a module called abc (abstract base class) that offers the necessary tools for crafting an abstract base class. First and foremost, you should understand the ABCMeta metaclass provided by the abstract base class. The rule is every abstract class must use ABCMeta metaclass.

What is the difference between a generic collection object and a sequence object in Python?

To conclude this Python Sequence and collection tutorial, we will say that a sequence has a deterministic ordering, but a collection does not. Examples of sequences include strings, lists, tuples, bytes sequences, bytes arrays, and range objects. Those of collections include sets and dictionaries.


1 Answers

The module originates from PEP-3119 which proposes:

Specific ABCs for containers and iterators, to be added to the collections module.

But since then, it evolved to be something more. And now a module description does not mention containters and iterators explictly. It says:

This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whether it is a mapping.

I believe it is a collections submodule just because it was a part of it in the past:

New in version 3.3: Formerly, this module was part of the collections module.

like image 58
viservolf Avatar answered Oct 03 '22 04:10

viservolf