
On Tue, Aug 31, 2021 at 7:22 AM Chris Angelico <rosuav@gmail.com> wrote:
On Tue, Aug 31, 2021 at 7:17 AM Nick Parlante <nick@cs.stanford.edu> wrote:
On what basis do you ascertain whether "==" would work correctly? Please explain.
Hi Chris, I'm just glancing at the line of code, and doing a little thought experiment to see if it would get the same output if == was used instead. For a singleton like None or False or the class like "list" .. == will return the same answer as "is". Look at these lines;
if type(items) is list:
Oh, and this one is a little more work to prove, but it can be done.
class Meta(type): ... def __eq__(self, other): return True ... class List(list, metaclass=Meta): pass ... items = List([1, 2, 3]) items [1, 2, 3] type(items) is list False type(items) == list True
(If you prefer, you could have items be a dict-like type instead of list-like, so it compares equal to list but doesn't behave like one. The demonstration is identical.) ChrisA