geekcomputers/Python · error · TypeError

The score_function argument must be a callable object.

Error message

The score_function argument must be a callable object.

What it means

Tubes' constructor validates that score_function is callable because it invokes the callback each time the bird passes a tube. Passing a non-callable raises this TypeError before the thread starts.

Source

Thrown at Flappy Bird - created with tkinter/Tubes.py:40

        bird,
        score_function=None,
        *screen_geometry,
        fp=("tube.png", "tube_mourth"),
        animation_speed=50,
    ):
        # Verifica os parâmetros passados e lança um erro caso algo esteja incorreto
        if not isinstance(background, Background):
            raise TypeError(
                "The background argument must be an instance of Background."
            )
        if not len(fp) == 2:
            raise TypeError(
                "The parameter fp should be a sequence containing the path of the images of the tube body and the tube mouth."
            )
        if not isinstance(bird, Bird):
            raise TypeError("The birdargument must be an instance of Bird.")
        if not callable(score_function):
            raise TypeError("The score_function argument must be a callable object.")

        Thread.__init__(self)

        # Instância os parâmetros
        self.__background = background
        self.image_path = fp
        self.__animation_speed = animation_speed
        self.__score_method = score_function

        # Recebe a largura e altura do background
        self.__width = screen_geometry[0]
        self.__height = screen_geometry[1]

        # Recebe o tamanho do pássaro
        self.__bird_w = bird.width
        self.__bird_h = bird.height

        # Calcula a largura e altura da imagem

View on GitHub (pinned to 40f4cd2652)

Solutions

  1. Pass the function reference without parentheses: Tubes(bg, bird, self.increase_score)
  2. If the scorer is a method of another object, pass obj.increase_score
  3. Use a lambda or partial when extra arguments are needed

Example fix

# before
tubes = Tubes(bg, bird, self.increase_score())
# after
tubes = Tubes(bg, bird, self.increase_score)
Defensive patterns

Strategy: type-guard

Validate before calling

if not callable(score_function):
    raise TypeError('score_function must be a function reference')

Type guard

def is_callable(fn) -> bool:
    return callable(fn)

Try / catch

try:
    tubes = Tubes(bg, bird, self.increase_score)
except TypeError as e:
    print('score_function must be callable:', e)

Prevention

When it happens

Trigger: Passing score() (the return value) instead of score, a string, an int, or None as score_function.

Common situations: Calling the callback instead of referencing it; renaming the score method and passing a stale attribute; wiring a property/attribute that holds a value rather than a function.

Related errors


AI-assisted analysis of geekcomputers/Python@40f4cd2652 (2026-08-27). Data as JSON: /api/errors/2a87a512a5285fc3. Report an issue: GitHub.