Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bool
#1
cmd_array[] contains ['EVENT_EXTANT_WINDOW', '82', '1', '10', '0']

bool_expected = bool(cmd_array[4])
returns True

Why?

Thanks
Reply
#2
The general idea of bool is that if it's empty, it's False. 0, the number, is empty, so it's False. '0', the string, is not empty: it has one character. So it's True.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for posting.

So the numbers in the list are strings or an object? Should I cast it to int first?

bool_expected = bool(int(cmd_array[4]))
Reply
#4
In Python, everything is an object and almost any container can store objects of different types/classes. Even functions and custom class instances.
For example:

>>> def double(num):
...     return num + num
... 
>>> a_list = ['This is a string object', True, None, 0, -14.5, 42, 0 , double, '11']
>>> for element in a_list:
...     print(type(element))
... 
<class 'str'>
<class 'bool'>
<class 'NoneType'>
<class 'int'>
<class 'float'>
<class 'int'>
<class 'int'>
<class 'function'>
<class 'str'>
So the first element is a string obviously as the last one. There is a None, a boolean type, some integers, a float and a function which you can point to and call it from the list:

>>> a_list[-2](8)
16
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Everything in your list is a string. So if you want the zeros in the list to be False, you either need to convert them to ints, or you need to explicitly test for the string '0' (cmd_array[4] != '0').
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
I wanted to convert the string numbers to 1 or zero but this did not work, it still gave me True.
expected = bool(int(cmd_array[4]))

It looks like bool reports whether or not it exists and not zeroness.

So I switched to:

expected = True if int(cmd_array[4]) > 0 else False

and that works and thus my whole test function now works.

So hopefully I will now go back to other languages and leave you folks alone.

Thanks.
Reply
#7
try bool('0'),bool(0) and see what you get.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Bool Object is not Subscriptable quest 1 4,186 May-02-2021, 11:12 AM
Last Post: Yoriz
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() Rejoice 6 3,308 Aug-25-2020, 03:50 PM
Last Post: Rejoice
  bool b = (num == 100) this doesn't work? MelonMusk 2 2,018 Jun-12-2020, 02:18 AM
Last Post: bowlofred
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() bongielondy 2 14,161 Nov-27-2019, 03:25 PM
Last Post: ThomasL
  [split] Set bool variable RedSkeleton007 1 2,732 Oct-22-2017, 09:28 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020