Reputation: 66667
class a(object):
def a(self):
return True
__contains__=a
b=a()
print 2 in b#why error
Upvotes: 1
Views: 327
Reputation: 46228
__contains__
is meant to take an argument. a
doesn't accept an argument.
The following is your example with a working __contains__
:
>>> class a(object):
... def a(self, item):
... return True
... __contains__=a
...
>>> b=a()
>>> print 2 in b
True
Upvotes: 7
Reputation: 96746
The signature of __contains__
is:
object.__contains__(self, item)
as per documentation. You need to extend your "a" method:
def a(self, item)
class a(object):
def a(self, item):
return True
__contains__=a
Upvotes: 3