Closed asnramos closed 2 years ago
This warning about '"is" with a literal' a bit confusing. Technically you may ignore it, but I'll discuss what it actually means.
aliens.py
code makes use of Python's is operator rather than the ==
"is equal to" comparator, presumably because this makes for a nice English-like expression: enemyAtThisPosition is 1
is
is comparing is that the identity of enemyAtThisPosition
is the same as the identity of the literal number 1
True
and False
), this is fine to do, but not always for String values, which is why Python is warning us (even though we aren't comparing Strings)So, what is this identity, and why is it not always "safe" to test it for Strings?
Well, the identity is best understood as a concept of the actual thing being looked at. In a computer, it's the actual place in memory of the thing. Python will automatically consider all numbers to be the same actual numbers (all 1
s are the same 1
, all True
ths are the same).
So enemyAtThisPosition is 1
is not exactly the same as saing enemyAtThisPosition == 1
. Here, we test that enemyAtThisPosition
has the same value as 1
. But in Python enemyAtThisPosition
also is the number 1
.
But this distinction is so pedantic as to be confusing.
For Strings, though (and other Objects), you can't say 'apple' is 'apple'
and expect True
. They could be two different Strings, with the same value. They are equal still, but not the same.
You can intern a String, for a performance boost, so that there is only one instance of a String: the String and the literal strings in the code are the same string, but you won't avoid the warning.
Warning (from warnings module): File "C:\python-codigo\Games-with-Pygame-master\Games-with-Pygame-master\Part 10\aliens.py", line 65 if enemyAtThisPosition is 1: SyntaxWarning: "is" with a literal. Did you mean "=="?