seanmtracey / Games-with-Pygame

The code for my "Making games with PyGame" series for Raspberry Pi's The MagPi.
https://smt.codes/writings
85 stars 71 forks source link

Part 10\aliens.py #23

Closed asnramos closed 2 years ago

asnramos commented 3 years ago

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 "=="?

Warning (from warnings module): File "C:\python-codigo\Games-with-Pygame-master\Games-with-Pygame-master\Part 10\aliens.py", line 83 if mouseStates[0] is 1 and mouseDown is False: SyntaxWarning: "is" with a literal. Did you mean "=="?

Warning (from warnings module): File "C:\python-codigo\Games-with-Pygame-master\Games-with-Pygame-master\Part 10\aliens.py", line 86 elif mouseStates[0] is 0 and mouseDown is True: SyntaxWarning: "is" with a literal. Did you mean "=="?

Warning (from warnings module): File "C:\python-codigo\Games-with-Pygame-master\Games-with-Pygame-master\Part 10\aliens.py", line 194 if mouseStates[0] is 1: SyntaxWarning: "is" with a literal. Did you mean "=="?

Warning (from warnings module): File "C:\python-codigo\Games-with-Pygame-master\Games-with-Pygame-master\Part 10\aliens.py", line 200 elif mouseStates[0] is 0 and mouseDown is True: SyntaxWarning: "is" with a literal. Did you mean "=="?

Warning (from warnings module): File "C:\python-codigo\Games-with-Pygame-master\Games-with-Pygame-master\Part 10\aliens.py", line 207 if gameStarted is True and gameWon is True and len(enemyShips) is 0: SyntaxWarning: "is" with a literal. Did you mean "=="?

milohax commented 3 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.

  1. The 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
  2. What is is comparing is that the identity of enemyAtThisPosition is the same as the identity of the literal number 1
  3. For Numbers, and Booleans (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 1s are the same 1, all Trueths 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.