Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isinstance of bool?

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?

like image 903
Ramy Avatar asked Apr 19 '11 14:04

Ramy


People also ask

Is instance of bool Python?

Summary. Python uses the bool class to represent boolean values: True and False . True and False are instances of the bool class.

What does Isinstance mean in Python?

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.

Is boolean an int in Python?

And that's because, in Python, Booleans are a subtype of Integers, which means that isinstance(True, int) returns True 😳.

Is true an int Python?

6 Answers. Show activity on this post. For historic reasons, bool is a subclass of int , so True is an instance of int .


2 Answers

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'.

like image 108
orlp Avatar answered Sep 21 '22 05:09

orlp


As nightcracker said, command line arguments are strings.
You can use sys.argv[2] in ('True', 'False').

like image 27
wong2 Avatar answered Sep 18 '22 05:09

wong2