Python"NoneType不可调用"错误

ty.*_*ty. 4 python types

我有一个看起来如下的函数,有很多可选参数.其中一个参数,在所有其他参数中,是text.

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

不确定是什么问题.我应该以不同的方式测试类型吗?在python命令行上进行试验似乎证实了我的做法应该有效.

Sil*_*ost 8

我猜你有一个名为typesomewhere 的参数,我可以使用以下代码轻松重现你的错误:

>>> type('abc')
<class 'str'>
>>> type = None
>>> type('abc')
Traceback (most recent call last):
  File "<pyshell#62>", line 1, in <module>
    type('abc')
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)