← index #7639Issue #7884
Related · high · value 1.245
QUERY · ISSUE

micropython dict has not __contains__ method

openby bhcuong2008opened 2021-08-11updated 2021-08-12
py-core

Hi Team,

Today I check contains method of dict, but not found. MicroPython v1.16. I open file objdict.c, and also not found. Is it missed?

Thanks,
Cuong.

CANDIDATE · ISSUE

CPython difference for __contains__ method

closedby stinosopened 2021-10-06updated 2022-04-20
py-core
class TestContains:
  def __contains__(self, arg):
    return arg

print(0 in TestContains())
print(1 in TestContains())
print('' in TestContains())
print(0 not in TestContains())
print(1 not in TestContains())
print('' not in TestContains())

CPython:

False
True
False
True
False
True

because 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.

MicroPython:

0
1

True
False
True

Unexpected results so definitely worthy of a cpydiff addition, or should this be fixed?

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied