TheaterFire

Not something I expected to be googling today...

Posted by beyphy@reddit | ProgrammerHumor | View on Reddit | 11 comments

Not something I expected to be googling today...

Reply to Post

11 Comments

madbirdribdam@reddit

`ast.literal_eval(“False”) == False`
View on Reddit #20931

smog29@reddit

Why would you compare it to false if it literally returns you a bool? You want to check if false==false?
View on Reddit #20932

madbirdribdam@reddit

I want to show that it returns False, man. When you see, that the whole express can be replaced with semantically equivalent construction, it’s not necessary because it’s written by a moron who can’t see that :)
View on Reddit #20933

Immediate-Win-3043@reddit

[link](https://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python) A freaking gold mine of everything from "why don't you just use an if else" to "here is this obscure module that has this one function that does it.", To "have you tried converting to Json or yml first?" And "here is a depreciated std library function from the depths of hell that performs an if else for you"
View on Reddit #15772

zyygh@reddit

The top answer gives a really great into what's going on though: >Empty strings evaluate to False, but everything else evaluates to True. So this should not be used for any kind of parsing purposes. This makes perfect sense. That "bool()" constructor doesn't **parse** strings, it **converts** them. It is very logical for non-zero values to be converted to True. If your string is "False" or "0" or "zero" or "nope", all this constructor sees is that it has contents and hence is non-zero. If you go in with the assumption that it will interpret the contents of your string, I can understand that you get stuck for hours on an issue like this. Easy mistake to make, even for experienced programmers.
View on Reddit #15773

vlsdo@reddit

Yeah you basically want to do some kind of eval("False")
View on Reddit #15774

anonymoussphenoid@reddit

the safer version would be `ast.literal_eval("False")` because it won't eval arbitary code... only literals.
View on Reddit #15775

er3z7@reddit

Added to the list of comments i should have seen before knowingly making bad code for a lack of an easy alternative
View on Reddit #15776

cs12345@reddit

I’m really curious, why are booleans stored in string format such a common problem for people? This is something I maybe encountered once, and never in any sort of realistic scenario.
View on Reddit #15777

misingnoglic@reddit

Probably accepting user input or reading a string.
View on Reddit #15778

nemo24601@reddit

Also evaluating environment vars
View on Reddit #15779