Is it bad I don't use boolean True / False in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

22nd Jan 2017, 5:28 AM
Ahri Fox
Ahri Fox - avatar
13 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.
24th Jan 2017, 12:29 PM
Mark Foxx
Mark Foxx - avatar
+ 4
no it's not necessary to use boolean
22nd Jan 2017, 12:27 PM
shashank chouhan
shashank chouhan - avatar
+ 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.
23rd Jan 2017, 11:20 AM
Niels Koomen
Niels Koomen - avatar
+ 4
Using numbers seems more bad, True and False are normal
24th Jan 2017, 11:58 AM
Kebap
Kebap - avatar
+ 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... ;-)
24th Jan 2017, 8:56 PM
Freek de Bruijn
Freek de Bruijn - avatar
+ 3
no, it's n't bad bro ;)
23rd Jan 2017, 1:31 PM
Prabhudutta Satapathy
Prabhudutta Satapathy - avatar
+ 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 :)
23rd Jan 2017, 5:39 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
using booleans makes your code more simple to be understood, but using only numbers its ok too.
23rd Jan 2017, 11:24 AM
Kleber Leal
Kleber Leal - avatar
+ 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!
24th Jan 2017, 5:10 PM
J. Alvaro Fernandez Muñoz
J. Alvaro Fernandez Muñoz - avatar
+ 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).
25th Jan 2017, 8:59 AM
Daniel
Daniel - avatar
+ 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.
26th Jan 2017, 6:01 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
I think not, because it's not necessary like said Shashank.
23rd Jan 2017, 11:09 AM
Ruslan Bayramov
Ruslan Bayramov - avatar
0
No
24th Jan 2017, 5:07 AM
Vishnu Sivan
Vishnu Sivan - avatar