Вещи Python, которые не являются ни Верными, ни Ложными

  1. , если это возможно, попросите соединяться с кем-то
  2. всегда количество к 3 прежде, чем нажать Enter (если один, поскольку это приведет Вашего парного партнера в бешенство!)
12
задан SilentGhost 18 October 2009 в 12:15
поделиться

6 ответов

a is a one-member tuple, which evaluates to True. is test identity of the object, therefore, you get False in all those test. == test equality of the objects, therefore, you get False again.

in if statement a __bool__ (or __nonzero__) used to evaluate the object, for a non-empty tuple it should return True, therefore you get True. hope that answers your question.

edit: the reason True and False are equal to 1 and 0 respectively is because bool type implemented as a subclass of int type.

9
ответ дан 2 December 2019 в 05:27
поделиться

Things in python don't have to be one of True or False.

When they're used as a text expression for if/while loops, they're converted to booleans. You can't use is or == to test what they evaluate to. You use bool( thing )

>>> a = (None,)
>>> bool(a)
True

Also note:

>>> 10 == True
False
>>> 10 is True
False
>>> bool(10)
True
2
ответ дан 2 December 2019 в 05:27
поделиться

(None,) is a tuple that contains an element, it's not empty and therefore does not evaluate to False in that context.

1
ответ дан 2 December 2019 в 05:27
поделиться

In Python every type can be converted to bool by using the bool() function or the __nonzero__ method.

Examples:

  • Sequences (lists, strings, ...) are converted to False when they are empty.
  • Integers are converted to False when they are equal to 0.
  • You can define this behavior in your own classes by overriding __nonzero__().

[Edit]

In your code, the tuple (None,) is converted using bool() in the if statements. Since it's non-empty, it evaluates to True.

0
ответ дан 2 December 2019 в 05:27
поделиться

I find almost all the explanations here unhelpful, so here is another try:

The confusion here is based on that testing with "is", "==" and "if" are three different things.

  • "is" tests identity, that is, if it's the same object. That is obviously not true in this case.
  • "==" tests value equality, and obviously the only built in objects with the values of True and False are the object True and False (with the exception of the numbers 0 and 1, of any numeric type).

And here comes the important part:

  • 'if' tests on boolean values. That means that whatever expression you give it, it will be converted to either True or False. You can make the same with bool(). And bool((None,)) will return True. The things that will evaluate to False is listed in the docs (linked to by others here)

Now maybe this is only more clear in my head, but at least I tried. :)

13
ответ дан 2 December 2019 в 05:27
поделиться

Поскольку a = (None,) является кортежем, содержащим единственный элемент None

Повторите попытку с a = None и вы увидите другой результат.

Также попробуйте a = () , который является пустым кортежем. Это истинное значение ложно

1
ответ дан 2 December 2019 в 05:27
поделиться
Другие вопросы по тегам:

Похожие вопросы: