开发者

Python "NoneType is not callable" error

开发者 https://www.devze.com 2023-01-08 11:14 出处:网络
I have a function that looks like the following, with a whole lot of optional parameters.One of these parameters, somewhere amidst all the others, is text.

I have a function that looks like the following, with a whole lot of optional parameters. One of these parameters, somewhere amidst all the others, is text.

I handle text specially because if it is a boolean, then I want to run to do something based on that. If it's not (which means it's just a string), then I do something else. The code looks roughly like this:

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

I get the following error on the type(text)==bool line:

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

Not sure what the problem is. Should I be testing the type differently? Experimenting on the python command line seems to confirm that my way of doing it should work.


I guess you have an argument called type somewhere, I can easily reproduce your error with the following code:

>>> 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


I bet you have a type=None among your arguments.

Just a special case of the general rule: "don't hide built-in identifiers with your own -- it may or may not bite in any specific give case, but it will bite you nastily in some cases in the future unless you develop the right habit about it"!-)

0

精彩评论

暂无评论...
验证码 换一张
取 消