Why does assigning to True/False not work as I expect?

后端 未结 5 2049
甜味超标
甜味超标 2020-11-27 21:39

As part of answering another question, I wrote the following code whose behaviour seems bizarre at first glance:

print True                    # outputs true         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 21:49

    Imagine this instead:

    A = True
    B = False
    
    print A           # true
    A = B;  print A   # false
    A = A;  print A   # false, because A is still false from before
    A = not A; print A # true, because A was false, so not A is true
    

    The exact same thing is going on, but in your version it's confusing, because you don't expect that you can redefine True and False.

提交回复
热议问题