这个函数(在python3中)会返回True吗?
def p(a,b):
return (a in b and b in a)
答案 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)