Python error "NoneType not callable"

I have a function that looks like this, with many additional parameters. One of these options, somewhere among all the others, is text .

I am processing text specifically, because if it is logical, then I want to run in order to do something based on this. If this is not the case (this means that it is just a string), then I am doing something else. The code looks something 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 in the line 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 

I don’t know what the problem is. Should I test type differently? The python command line experiment seems to confirm that my way of doing this should work.

+4
source share
2 answers

I assume you have a type argument 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 
+8
source

I am sure you have type=None among your arguments.

Just a special case of the general rule: "do not hide the built-in identifiers with your own - it may or may not bite in any particular case, but it will bite you disgusting in some cases in the future if you do not develop the right habit about it !! )

0
source

Source: https://habr.com/ru/post/1316193/


All Articles