My scenarios where I need it:
User is inputting True or False and it is parsed as str by default. I cannot change the parsing type as python 3+ parses as str (Using Python 3.8) So when I parse
bool(input('Enter True or False'))
, it returnsTrue
regardless since default bool function only returnsFalse
when there is an empty string.True
otherwise.I have a json for which I need it.
It has following representation:
list_of_visits ='''{"Museum": "True",
"Library": "True",
"Park": "False"}
'''
Note that I cannot have its representation without qoutes as:
list_of_visits ='''{"Museum": True,
"Library": True,
"Park": False}
'''
Cause parsing this as follows throws error:
jsonic = json.loads(list_of_visits)
jsonic
But I need to parse from int and float as well at some places and I cannot write functions for each type separately . I am trying to build a one-stop solution which might inherit functionality from bool() and does this conversion as well and I can use it everywhere i.e. solution which can not only perform traditional bool() operations but also able to parse string representations.
Currently I do following steps:
if type(value) == str and value =='True':
reprs = True
elif type(value) == str and value =='False':
reprs = False
else:
reprs = bool(value)