Returning boolean if set is empty
not as pythonic as the other answers, but mathematics:
return len(c) == 0
As some comments wondered about the impact len(set)
could have on complexity. It is O(1) as shown in the source code given it relies on a variable that tracks the usage of the set.
static Py_ssize_t
set_len(PyObject *so)
{
return ((PySetObject *)so)->used;
}
def myfunc(a,b):
c = a.intersection(b)
return bool(c)
bool()
will do something similar to not not
, but more ideomatic and clear.