in Python, i'd like to check to make sure a command line argument is of type bool before I use it in a conditional statement. this: isinstance(sys.argv[2], bool)
is coming back false. What's the right way to do this?
Summary. Python uses the bool class to represent boolean values: True and False . True and False are instances of the bool class.
Python isinstance() Function The isinstance() function returns True if the specified object is of the specified type, otherwise False . If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.
And that's because, in Python, Booleans are a subtype of Integers, which means that isinstance(True, int) returns True 😳.
6 Answers. Show activity on this post. For historic reasons, bool is a subclass of int , so True is an instance of int .
All command line arguments are strings. Please refine what you want.
If you want to check for the argument true
, check if sys.argv[2]
equals 'true'
.
As nightcracker said, command line arguments are strings.
You can use sys.argv[2] in ('True', 'False')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With