python 特殊属性

目的 所编写代码 实际调用
判断某序列是否包含特定的值 x in seq seq.__contains __(x)

__contains __ 当使用in,not in 对象的时候调用 (not in 是在in完成后再取反,实际上还是in操作)

class NewList(object):

     def __init(self, values):

        self.values = values

     def __contains__(self, value):
           return value in self.values
 a = NewList([1, 2, 3, 4])
4 in a
True
8 in a
False

https://blog.csdn.net/business122/article/details/7568446

猜你喜欢

转载自blog.csdn.net/qq_29232389/article/details/86240052
今日推荐