A text book is demonstrating how the built in sum function can be implemented with error checking. Here are the first 2 lines,
I understand that with isinstance() method we are testing if
I tried to find the definition of collections.Iterable by ctrl+clicking on it on PyCharm. The IDE took me to the following section of code located in <python_env>\Lib\typing.py,
I am under the assumption that
At this point I am confused what is going on with the following code,
collections.Iterable seems to be a static variable of some sort in Typing module. But I was expecting a class name as a second argument in the isinstance method.
Right after the code, the book says the following, "The abstract base class, collections.Iterable, includes all of Python’s iterable containers types that guarantee support for the for-loop syntax..". I guess I am looking for the declaration of collections.Iterable class.
1 2 3 |
def sum (values): if not isinstance (values, collections.Iterable): # There is more, but I am guessing it's not relavent for the question |
values
variable are of a certain class type. My first guess is, Iterable is a class available in collections module, and I tried to verify that by following the definition.I tried to find the definition of collections.Iterable by ctrl+clicking on it on PyCharm. The IDE took me to the following section of code located in <python_env>\Lib\typing.py,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Various ABCs mimicking those in collections.abc. _alias = _SpecialGenericAlias Hashable = _alias(collections.abc.Hashable, 0 ) # Not generic. Awaitable = _alias(collections.abc.Awaitable, 1 ) Coroutine = _alias(collections.abc.Coroutine, 3 ) AsyncIterable = _alias(collections.abc.AsyncIterable, 1 ) AsyncIterator = _alias(collections.abc.AsyncIterator, 1 ) Iterable = _alias(collections.abc.Iterable, 1 ) # Takes me here Iterator = _alias(collections.abc.Iterator, 1 ) Reversible = _alias(collections.abc.Reversible, 1 ) Sized = _alias(collections.abc.Sized, 0 ) # Not generic. Container = _alias(collections.abc.Container, 1 ) Collection = _alias(collections.abc.Collection, 1 ) Callable = _CallableType(collections.abc. Callable , 2 ) Callable .__doc__ = \ |
_alias
is a function, but ctrl+clicking it take me to _SpecialGenericAlias
class's cosntructor.At this point I am confused what is going on with the following code,
1 |
isinstance (values, collections.Iterable): |
Right after the code, the book says the following, "The abstract base class, collections.Iterable, includes all of Python’s iterable containers types that guarantee support for the for-loop syntax..". I guess I am looking for the declaration of collections.Iterable class.