If someone already have experience with languages like C/C++, Java, C#
comparison between two variables of different types results in a compile error.
But in Python this will simply evaluate to false. So beware when comparing a string and int
containg same values. when we print, they look the same.
I actually faced this issue when I am receving a param from a config file
and comparing with some int value in the program something similar to the following.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
batch_size = cnf.get_batch_size() | |
batch = [] | |
#Some code to fill the batch | |
if len(batch) == batch_size: | |
#Flush out the batch by saving to DB | |
else: | |
#Some other code | |
#The Fix here is not to forget to convert the config parameter to int | |
batch_size = int(cnf.get_batch_size()) | |
num1 = "3" | |
num2 = 3 | |
print(num1) | |
print(num2) | |
if num1 == num2: | |
print("Equal") | |
else: | |
print("Not equal") |