nasa-jpl / autoRIFT

A Python module of a fast and intelligent algorithm for finding the pixel displacement between two images
Apache License 2.0
212 stars 52 forks source link

Potential for incorrect comparison with 'is' vs '==' #64

Closed snoyes closed 1 year ago

snoyes commented 2 years ago

https://github.com/nasa-jpl/autoRIFT/blob/2ed6e78a2c7e71b1ba4042efe9e9cbd34ffcfeeb/netcdf_output.py#L184

is compares object ids. It is possible for a variable to contain the string 'radar' without being the same object as another string 'radar'. Use == to compare objects to literals.

def compare(bar):
   print(f"{id(bar)=}")
   print(f"{id('radar')=}")
   print(f"{bar is 'radar'=}")
   print(f"{bar == 'radar'=}")

foo = 'rad'
foo += 'ar'
compare(foo)

id(bar)=2523833253296
id('radar')=2523833249968
bar is 'radar'=False
bar == 'radar'=True