What does __contains__ do, what can call __contains__ function

前端 未结 5 853
鱼传尺愫
鱼传尺愫 2020-11-30 01:17

Here is my code:

class a(object):
    d=\'ffffd\'
    def __contains__(self):
        if self.d:return True
b=a()
print b.contains(\'d\')  # error
print contai         


        
5条回答
  •  借酒劲吻你
    2020-11-30 01:53

    to get your code to do something (although nothing useful):

    class a(object):
    
        d = 'ffffd'
    
        def __contains__(self, m):
            if self.d: 
                return True
    
    b = a()
    
    >>> 'd' in b
    True
    

    The docs.

提交回复
热议问题