문제

I can't understand what is happening here?

True = False
False = True
print True, False

output

False False

Isn't the output be printed as False True?

도움이 되었습니까?

해결책

You are setting True = False, and then False = True.

True = False
False = True # But "True" here is now False.
print True, False # True = False, because of the first line. As does False, because you set it equal to "True" which you have already made == False.

I don't know why you would ever want to do this, other than to mess with someone's code, as it's a readability nightmare - as you can see just from the difficulty in using the words to explain it.

If you really want to swap the vaules around, then do:

True, False = False, True

다른 팁

You seem to be using Python 2.

This wouldn't work in Python 3 as True and False were changed to keywords in order to make assignment to those impossible.

Refer to Core Language Changes:

Make True and False keywords. [6]

Reason: make assignment to them impossible.

Once you do True = False, True is no longer a boolean but rather a variable that has been assigned the boolean False. Therefore, the line False = True is actually assigning True's value (False) to the variable False.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top