# HG changeset patch # User Paul Boddie # Date 1480436982 -3600 # Node ID 22aea7bc1acd80b91a8cb7e7ad1c2a627ae0840e # Parent d16731ea85682b0ee1dd95e675bf630dfaafb9be Moved __contains__ to the sequence module, providing an implementation. Added tests of "in", "not in" and operator syntax. diff -r d16731ea8568 -r 22aea7bc1acd lib/__builtins__/list.py --- a/lib/__builtins__/list.py Tue Nov 29 17:27:31 2016 +0100 +++ b/lib/__builtins__/list.py Tue Nov 29 17:29:42 2016 +0100 @@ -39,7 +39,6 @@ if args is not None: self.extend(args) - def __contains__(self, value): pass def __delitem__(self, index): pass def __setslice__(self, start, end, slice): pass def __delslice__(self, start, end): pass diff -r d16731ea8568 -r 22aea7bc1acd lib/__builtins__/sequence.py --- a/lib/__builtins__/sequence.py Tue Nov 29 17:27:31 2016 +0100 +++ b/lib/__builtins__/sequence.py Tue Nov 29 17:29:42 2016 +0100 @@ -46,6 +46,22 @@ return str(b) + def __contains__(self, value): + + "Return whether the list contains 'value'." + + # Perform a linear search of the sequence contents. + + for v in self: + + # Return True if the current value is equal to the specified one. + # Note that this is not an identity test, but an equality test. + + if v == value: + return True + + return False + def __getitem__(self, index): "Return the item or slice specified by 'index'." diff -r d16731ea8568 -r 22aea7bc1acd tests/list.py --- a/tests/list.py Tue Nov 29 17:27:31 2016 +0100 +++ b/tests/list.py Tue Nov 29 17:29:42 2016 +0100 @@ -24,3 +24,12 @@ print l[-5] # should raise an exception except IndexError, exc: print "l[-5]: failed with argument", exc.index + +print 1 in l # True +print 4 in l # False +print "four" in l # True +print "one" in l # False +print 1 not in l # False +print 4 not in l # True +print "four" not in l # False +print "one" not in l # True diff -r d16731ea8568 -r 22aea7bc1acd tests/operator_syntax.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/operator_syntax.py Tue Nov 29 17:29:42 2016 +0100 @@ -0,0 +1,4 @@ +print 1 == 1 # True +print 1 != 1 # False +print 1 == "one" # False +print 1 != "one" # True