Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python how to judge whether a variable is boolean type

Tags:

python

In python, how to judge whether a variable is bool type,python 3.6 using

    for i in range(len(data)):
        for k in data[i].keys():
            if type(data[i][k]) is types.BooleanType:
                data[i][k] = str(data[i][k])
            row.append(data[i][k])
            #row.append(str(data[i][k]).encode('utf-8'))
        writer.writerow(row)
        row = []

but it errors:

  if type(data[i][k]) is types.BooleanType:

  TypeError: 'str' object is not callable
like image 429
bin Avatar asked Jan 12 '17 09:01

bin


2 Answers

you can check type properly with isinstance()

isinstance(data[i][k], bool)

will return true if data[i][k] is a bool

like image 154
RichSmith Avatar answered Nov 16 '22 00:11

RichSmith


 isinstance(data[i][k], bool) #returns True if boolean

instead of :

if type(data[i][k]) is types.BooleanType:
like image 44
Taufiq Rahman Avatar answered Nov 15 '22 22:11

Taufiq Rahman