我有一个如下的函数,有很多可选参数。其中一个参数,在所有其他参数中的某处,是
text
我来处理
特别是因为如果它是一个布尔值,那么我想基于它运行。如果它不是(这意味着它只是一个字符串),那么我会做其他事情。代码大致如下:
def foo(self, arg1=None, arg2=None, arg3=None, ..., text=None, argN=None, ...):
...
if text is not None:
if type(text)==bool:
if text:
# Do something
else:
# Do something else
else:
# Do something else
我得到以下错误
type(text)==bool
生产线:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...", line 79, in foo
if type(text)==bool:
TypeError: 'NoneType' object is not callable
不知道是什么问题。我应该用不同的方式测试这个类型吗?在python命令行上进行的实验似乎证实了我的方法是可行的。