Python: False vs 0

In PHP, you use the notation ===for testing TRUEor FALSE, other than 1or 0.

For example if FALSE == 0returns TRUE, if FALSE === 0returns FALSE. Therefore, when performing string searches in database 0, if the position of the substring in the question of law is at the beginning, you get 0which PHP can distinguish from FALSE.

Is there a way to do this in Python?

+3
source share
3 answers

In python

  • The operator isverifies the identity ( False is False, 0 is not False).

  • An operator ==that checks logical equality (and therefore 0 == False).

PHP ===, - Python, a == b and type(a) is type(b).

is ==:

  • {} == {}, {} is not {} ( )
  • , a = {}, a is a ( )

  • "a"*255 is not "a"*255", "a"*20 is "a"*20 , - , Python . , , , is . "a"*255 == "a"*255 .

  • 12345 is 12345, 12345 is not 12345 + 1 - 1 . .
+14
if something is False:

- ,

if something is None:

- is... ( something is 123457 simillar)

ints , . http://ideone.com/iKmWCn

+4

x === y Python - type(x) is type(y) and x == y. , Python . , .

If you check the particular unique object, for example ( True, False, Noneor a class), you should use isand is not. For example: x is True.

0
source

All Articles