a和b的任何值,以便(a中的a和b中的b)== True

时间:2018-02-13 21:03:51

标签: python python-3.6

这个函数(在python3中)会返回True吗?

def p(a,b):
    return (a in b and b in a)

1 个答案:

答案 0 :(得分:9)

是的,一对包含彼此的列表:

a, b = [], []
a.append(b)
b.append(a)
print(a in b and b in a)

或者,包含自己的列表:

a = b = []
a.append(a)
print(a in b and b in a)

或者,__contains__操作员调用的<{1}}

in

或者,相等的字符串:

class Foo:
    def __contains__(self, other):
        return True

a = Foo()
b = Foo()
print(a in b and b in a)

或者,彼此产生的生成器:

a = 'foo'
b = 'foo'
print(a in b and b in a)

这是一个笑话,它永远不会终止:

def foo():
    x = yield
    while True:
        yield x

a = foo()
b = foo()
next(a)
next(b)
a.send(b)
b.send(a)
print(a in b and b in a)