Commit 58cbb4d6 authored by Damien George's avatar Damien George
Browse files

py: Implement __contains__ special method.

parent 62f7ba7a
......@@ -384,7 +384,9 @@ STATIC const qstr binary_op_method_name[] = {
[MP_BINARY_OP_IN] = MP_QSTR___contains__,
[MP_BINARY_OP_EXCEPTION_MATCH] = MP_QSTR_, // not implemented, used to make sure array has full size
# 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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment