Why does this str strict comparison fail in Python?
本问题已经有最佳答案,请猛点这里访问。
我在一个装饰器中有这个代码:
1 2 3 4 | is_local_valid = ['system_id', 'server_id', 'sensor_id'] local_params = [x for x in is_local_valid if x in kwargs.keys() and kwargs[x].lower() is 'local'] + [y for y in args if y.lower() is 'local'] if local_params == []: raise AssertionError("Tried to access the database from a non connected profile") |
我注意到,在这种情况下,用于比较两个字符串的
有什么线索吗?
The operators is and is not test for object identity:
x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.
1 2 3 4 5 | >>> id('local') 42745112 >>> a = {1: 'locAl'} >>> id(a[1].lower()) 53363408 |
它们不是同一物体
1 2 3 4 5 6 | >>>"".join("hello") 'hello' >>>"hello" is"".join("hello") False >>>"hello" =="".join("hello") True |
与
1 | if a is None: |