代码之家  ›  专栏  ›  技术社区  ›  ty.

Python“NoneType is not callable”错误

  •  4
  • ty.  · 技术社区  · 14 年前

    我有一个如下的函数,有很多可选参数。其中一个参数,在所有其他参数中的某处,是 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命令行上进行的实验似乎证实了我的方法是可行的。

    2 回复  |  直到 14 年前
        1
  •  8
  •   SilentGhost    14 年前

    我猜你有个论点叫 type 在某个地方,我可以用以下代码轻松地重现您的错误:

    >>> 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
    
        2
  •  0
  •   Alex Martelli    14 年前

    我打赌你有一个 type=None 在你的论点中。

    在将来的某些情况下,除非你养成正确的习惯,否则你会恶毒地咬你“!-)