开发者

i have '__contains__' ,why error

开发者 https://www.devze.com 2022-12-15 22:21 出处:网络
class a(object): def a(self): return True __contains__=a b=a()开发者_开发知识库 print 2 in b#why error
class a(object):
    def a(self):
        return True
    __contains__=a

b=a()开发者_开发知识库
print 2 in b#why error


__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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消