Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "NoneType is not callable" error

Tags:

python

types

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.

like image 736
ty. Avatar asked Feb 26 '23 15:02

ty.


1 Answers

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
like image 132
SilentGhost Avatar answered Mar 08 '23 16:03

SilentGhost