I have a function:
def f(a):
return 100 in a
I want to annotate the argument a
, for example
from typing import List
def f(a: List[int]):
return 100 in a
But types which support in
operator is not only list
but also set
or tuple
(or maybe dict
or keyview
or something).
My question is:
What type is the best for a
, which support in
operator?
As in, which is the simplest type which provides this particular functionality?
It seems that typing.Container
or typing.Collection
is ok, but I have no idea about which type is the best.
I would choose Union[Container, Iterable]
. The python documentation for membership testing states that it first tries __contains__
then it tries __iter__
to determine membership. It also states that it tries __getitem__
failing both but only for old style iteration. To be honest I'm not sure what that means but I think we're safe here to ignore it.
Your arguments should be as generic as possible. If you chose Collection
that would prohibit generators since they don't implement len
for example, yet generators support the in
keyword.
https://docs.python.org/3/library/collections.abc.html#collections.abc.Container