hveagle 发表于 2022-10-15 10:46:57

一个小实验(x is y == True)

本帖最后由 hveagle 于 2022-10-15 10:48 编辑

>>> def is_True(demo):
        demo = demo + ' == True'
        result = eval(demo)
        if result == True:
                print('Is True.')
        else:
                print('Is False.')

       
>>> x = 'ABC'
>>> y = 'ABC'
>>> is_True('x is y')
Is False.
>>> def is_True(demo):
        demo = demo + ' == True'
        result = eval(demo)
        print(result)

       
>>> is_True('x is y')
False
>>> x is y == True
False
>>> x is y
True
>>>

临时号 发表于 2022-10-15 10:54:27

运算符优先级的问题,==的优先级比is高,会先算'ABC' == True,在算'ABC' is False
>>> ('ABC' is 'ABC') == True
True

hveagle 发表于 2022-10-15 12:32:52

x is y == True
x is False
False
页: [1]
查看完整版本: 一个小实验(x is y == True)