What is the right way to compare if a variable contains a callable I want? Should I use the is operator or the == operator?
In the following interactive session, I define two callables, f and g. I assign one to variable a and another to variable b.
Now, when I want to check if a is same as f or not, both a == f and a is f work.
>>> def f():
... pass
...
>>> def g():
... pass
...
>>> a = f
>>> b = g
>>> a == f
True
>>> a is f
True
>>> a == g
False
>>> a is g
False
>>> b == f
False
>>> b is f
False
>>> b == g
True
>>> b is g
True
My questions.
- Is using one operator better than the other?
- Is there a situation where one operator would produce a different result than the other?