TryBugHunt

TypeError: 'NoneType' object is not callable

TypeError: 'NoneType' object is not callable in Python means you put brackets () after something that is None. Usually you stored the result of calling a function (which returned None) where you meant to store the function itself.

PythonPattern: Null and undefined errors

How do I fix TypeError: 'NoneType' object is not callable?

TypeError: 'NoneType' object is not callable means you put () after a name that holds None. Usually you stored a function's result instead of the function, as in handler = greet(). Remove the brackets: handler = greet. Also check that a decorator returns its wrapper, and that no attribute shares a method's name.

  1. 1. Find the assignmentSearch for where the name before ( was set. If it says x = func(), remove the brackets.
  2. 2. Check decorators returnA decorator must end with return wrapper. Without it, the decorated function becomes None.
  3. 3. Check self attributesself.update = None in __init__ hides the update method. Give the attribute a different name.

What does TypeError: 'NoneType' object is not callable mean?

None is Python's "nothing" value. A function with no return statement gives back None. If you write handler = greet() instead of handler = greet, you have run greet once and kept its result, None. Calling handler() later then tries to call None.

Why does TypeError: 'NoneType' object is not callable happen?

1. Brackets when storing a function

greet() runs the function now. greet (no brackets) is the function itself.

Breaks

def greet():
    print("Hi")

handler = greet()
handler()

Works

def greet():
    print("Hi")

handler = greet
handler()

2. An attribute with the same name as a method

self.update = None replaces the update method for that object.

Breaks

class Game:
    def __init__(self):
        self.update = None
    def update(self):
        print("tick")

Game().update()

Works

class Game:
    def __init__(self):
        self.last_update = None
    def update(self):
        print("tick")

Game().update()

3. A decorator that forgets to return the wrapper

A decorator replaces the function with whatever it returns. No return means None.

Breaks

def log(func):
    def wrapper():
        print("calling")
        func()

@log
def run():
    pass

run()

Works

def log(func):
    def wrapper():
        print("calling")
        func()
    return wrapper

@log
def run():
    pass

run()

How to find it in your own code

Checklist: 1) Find the name just before the ( on the failing line. 2) print(name) above it; it will show None. 3) Search for where that name was assigned. 4) If it was assigned with brackets, like x = func(), remove them. 5) If it is self.something, check nothing in __init__ sets an attribute with the same name as a method.

Practise null and undefined errors

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.