+ 12
Is it bad I don't use boolean True / False in Python?
I've never found a use for it, and instead use numbers. Am I missing something huge for never using it in my code?
14 Answers
+ 6
Depending on the language, you need to make sure that the return is what your expecting and not of the same value. Programmers often go wrong when they expect 1 (int boolean) === exactly to the return, but then use == equal to instead. It would work, but they are not EXACTLY a match. That's a Javascript example.
+ 4
no it's not necessary to use boolean
+ 4
It is good design practice to avoid boolean flags if you can. It is better to save states within your object structure. For instance: If you have two states, administrator login and user login. You can use an abstract session class and you inherit from that class with an administrator session or a user session class. That is much safer than a flag like: isAdmin=True. Booleans are stored as int, so no memory advantages either.
+ 4
Using numbers seems more bad, True and False are normal
+ 4
Perhaps other programmers that try to understand your code might be missing something? If a variable can either be true or false, I would use the official predefined values when assigning to that variable. It makes the code more readable for me. The bool type was added to Python in 2002 (after much discussion; see https://www.python.org/dev/peps/pep-0285/ for a lot of considerations and background information).
So personally I do like to use the True and False keywords, for example when assigning to boolean variables and when specifying default values for parameters. This is a matter of code style, so do whatever you prefer and let's not argue about it... ;-)
+ 3
no, it's n't bad bro ;)
+ 3
Conditionals syntax is a bit simpler when you use booleans, but other than that, using numbers is potentially better:
When your program grows, down the road it might turn out you have to have more than two states of an important variable. If you had used it as "is/isn't", you're pretty much screwed :)
+ 2
using booleans makes your code more simple to be understood, but using only numbers its ok too.
+ 2
Boolean is a yes/no kind of data type. So it should be used for logical expressions. Numbers can be represented by other var types ... int and float!
+ 2
Boolean and integer types are the same size in Python (apparently 24 bytes each) so it doesn't matter in that aspect. Be careful with other programming languages as some will use less memory for booleans than for integer types.
Readabilty is probably the biggest hit you'll take by using integers. It won't be clear right away that the result is binary (true or false) so you may need to include more comments saying what the values are and what they represent. Readabilty is really important even if you are the only one reading it. Go back to old code you wrote a few years ago and you might have trouble reading your own code.
Additional Info:
C language doesn't actually have a boolean datatype. They use numbers. 0 is false and any number not 0 is true. In C, I personally use a char datatype and always include bool somewhere in the variable name so I know what it is. I use char because it is the smallest integer datatype in C (1 byte compared to 2-4 bytes when using int).
+ 2
Not exactly...as long as you test consistently (don't check for 97 when 1 is the expected test) and you assert that the concept of False is 'the absence of value'.
Not to be trivial, because this example doesn't apply to Python...from the Boolean direction the value for True in Visual Basic (cast to Integer) is -1; here your preference isn'tâ compatible with the purpose of "True/False": abstraction.
+ 1
I think not, because it's not necessary like said Shashank.
0
No