circuitpython/tests/basics/class_contains.py
Jon Bjarni Bjarnason 1ded8a2977 py/objtype: Convert result of user __contains__ method to bool.
Per https://docs.python.org/3/reference/expressions.html#membership-test-operations

    For user-defined classes which define the contains() method, x in y
    returns True if y.contains(x) returns a true value, and False
    otherwise.

Fixes issue #7884.
2022-04-20 15:44:46 +10:00

48 lines
788 B
Python

# A contains everything
class A:
def __contains__(self, key):
return True
a = A()
print(True in a)
print(1 in a)
print(() in a)
# B contains given things
class B:
def __init__(self, items):
self.items = items
def __contains__(self, key):
return key in self.items
b = B([])
print(1 in b)
b = B([1, 2])
print(1 in b)
print(2 in b)
print(3 in b)
class C:
def __contains__(self, arg):
return arg
print(C().__contains__(0))
print(C().__contains__(1))
print(C().__contains__(''))
print(C().__contains__('foo'))
print(C().__contains__(None))
print(0 in C())
print(1 in C())
print('' in C())
print('foo' in C())
print(None in C())
print(0 not in C())
print(1 not in C())
print('' not in C())
print('foo' not in C())
print(None not in C())