I have this piece of code that reads from a gunzip stream and checks if each line contains some pattern. What I have is
if (pattern in line):
do_something()
Some lines contain non-ASCII characters, when my code reaches those lines, I get a UnicodeDecodeError. However, I am unable to reproduce this error in my manual testing. When I copy the repr of the line that causes UnicodeDecodeError and assign it to variable line and do pattern in line, I get False instead of an error. I am confused about this inconsistency. Why does it behave different for the same string?
repr(some_string)is not the same assome_string, it is a representation of it. For some types it aspires to give a representation that can be used to construct a new instance of the type if given as input to an interpreter or as code, but not for all. See stackoverflow.com/questions/7784148/….