Having a problem returning True to a function call, what can i do so it returns True?
When i call math() it runs the function but when it exits i get the type error.
here is a snippet of my code:
def math_battle1():
solved = False
operations = ['+', '-']
ops = random.choice(operations)
num_list = []
tries = 3
a = random.randint(0, 20)
b = random.randint(0,20)
num_list.extend([a, b])
num_list.sort(reverse = True)
user_solution = int(input(f"Solve for: {num_list[0]} {ops} {num_list[1]} = "))
solution = eval(str(num_list[0]) + ops + str(num_list[1]))
if tries > 1:
if user_solution == solution:
print("nCorrect!")
solved = True
return solved
elif user_solution != solution:
tries = tries - 1
print(f"nIncorrect! You have {tries} more attempts.")
user_solution = int(input(f"nSolve for: {num_list[0]} {ops} {num_list[1]} = "))
else:
if tries <= 1 and user_solution != solution:
tries = tries -1
print(f"nThe correct solution is: {num_list[0]} {ops} {num_list[1]} = {solution}")
print("nGame Over.")
else:
print("nCorrect!")
solved = True
return solved
title_screen()
skill_menu()
option = get_user_input()
if option == 1:
math = math_battle1()
elif option == 2:
math = math_battle2()
math()
I followed your book to learn flask, it works very well until I used current_user , I got
AttributeError: 'bool' object has no attribute '__call__'
this kind of error. I thought maybe there’re some mistakes in my code, so I download your code from github, and checkout to branch 8c, but same thing happend, check traceback message below:
root@fb-kali:/opt/flasky# python manage.py runserver --port 8888 --host 0.0.0.0 --debug * Running on http://0.0.0.0:8888/ * Restarting with reloader 127.0.0.1 - - [16/Sep/2015 16:29:42] "GET / HTTP/1.1" 500 - Traceback (most recent call last): File "/usr/share/pyshared/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/usr/share/pyshared/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/share/pyshared/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/usr/share/pyshared/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/usr/share/pyshared/flask/app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/share/pyshared/flask/app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/share/pyshared/flask/app.py", line 1473, in full_dispatch_request rv = self.preprocess_request() File "/usr/share/pyshared/flask/app.py", line 1666, in preprocess_request rv = func() File "/opt/flasky/app/auth/views.py", line 14, in before_request if current_user.is_authenticated() TypeError: 'bool' object is not callable
Here is the version of some related package I’m using:
Python 2.7.9
Flask==0.10.1
Flask-Bootstrap==3.3.5.6
Flask-Login==0.3.0
Flask-Mail==0.9.1
Flask-Migrate==1.5.1
Flask-Moment==0.5.1
Flask-SQLAlchemy==2.0
Flask-Script==2.0.5
Flask-WTF==0.12
Jinja2==2.7.3
WTForms==2.0.2
Werkzeug==0.9.6
Thank you so much if you can help me solve this problem.
Have you ever seen the TypeError object is not callable when running one of your Python programs? We will find out together why it occurs.
The TypeError object is not callable is raised by the Python interpreter when an object that is not callable gets called using parentheses. This can occur, for example, if by mistake you try to access elements of a list by using parentheses instead of square brackets.
I will show you some scenarios where this exception occurs and also what you have to do to fix this error.
Let’s find the error!
What Does Object is Not Callable Mean?
To understand what “object is not callable” means we first have understand what is a callable in Python.
As the word callable says, a callable object is an object that can be called. To verify if an object is callable you can use the callable() built-in function and pass an object to it. If this function returns True the object is callable, if it returns False the object is not callable.
callable(object)
Let’s test this function with few Python objects…
Lists are not callable
>>> numbers = [1, 2, 3]
>>> callable(numbers)
False
Tuples are not callable
>>> numbers = (1, 2, 3)
>>> callable(numbers)
False
Lambdas are callable
>>> callable(lambda x: x+1)
True
Functions are callable
>>> def calculate_sum(x, y):
... return x+y
...
>>> callable(calculate_sum)
True
A pattern is becoming obvious, functions are callable objects while data types are not. And this makes sense considering that we “call” functions in our code all the time.
What Does TypeError: ‘int’ object is not callable Mean?
In the same way we have done before, let’s verify if integers are callable by using the callable() built-in function.
>>> number = 10
>>> callable(number)
False
As expected integers are not callable 🙂
So, in what kind of scenario can this error occur with integers?
Create a class called Person. This class has a single integer attribute called age.
class Person:
def __init__(self, age):
self.age = age
Now, create an object of type Person:
john = Person(25)
Below you can see the only attribute of the object:
print(john.__dict__)
{'age': 25}
Let’s say we want to access the value of John’s age.
For some reason the class does not provide a getter so we try to access the age attribute.
>>> print(john.age())
Traceback (most recent call last):
File "callable.py", line 6, in <module>
print(john.age())
TypeError: 'int' object is not callable
The Python interpreter raises the TypeError exception object is not callable.
Can you see why?
That’s because we have tried to access the age attribute with parentheses.
The TypeError‘int’ object is not callable occurs when in the code you try to access an integer by using parentheses. Parentheses can only be used with callable objects like functions.
What Does TypeError: ‘float’ object is not callable Mean?
The Python math library allows to retrieve the value of Pi by using the constant math.pi.
I want to write a simple if else statement that verifies if a number is smaller or bigger than Pi.
import math
number = float(input("Please insert a number: "))
if number < math.pi():
print("The number is smaller than Pi")
else:
print("The number is bigger than Pi")
Let’s execute the program:
Please insert a number: 4
Traceback (most recent call last):
File "callable.py", line 12, in <module>
if number < math.pi():
TypeError: 'float' object is not callable
Interesting, something in the if condition is causing the error ‘float’ object is not callable.
Why?!?
That’s because math.pi is a float and to access it we don’t need parentheses. Parentheses are only required for callable objects and float objects are not callable.
>>> callable(4.0)
False
The TypeError‘float’ object is not callable is raised by the Python interpreter if you access a float number with parentheses. Parentheses can only be used with callable objects.
What is the Meaning of TypeError: ‘str’ object is not callable?
The Python sys module allows to get the version of your Python interpreter.
Let’s see how…
>>> import sys
>>> print(sys.version())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
No way, theobject is not callable error again!
Why?
To understand why check the official Python documentation for sys.version.

That’s why!
We have added parentheses at the end of sys.version but this object is a string and a string is not callable.
>>> callable("Python")
False
The TypeError‘str’ object is not callable occurs when you access a string by using parentheses. Parentheses are only applicable to callable objects like functions.
Error ‘list’ object is not callable when working with a List
Define the following list of cities:
>>> cities = ['Paris', 'Rome', 'Warsaw', 'New York']
Now access the first element in this list:
>>> print(cities(0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
What happened?!?
By mistake I have used parentheses to access the first element of the list.
To access an element of a list the name of the list has to be followed by square brackets. Within square brackets you specify the index of the element to access.
So, the problem here is that instead of using square brackets I have used parentheses.
Let’s fix our code:
>>> print(cities[0])
Paris
Nice, it works fine now.
The TypeError‘list’ object is not callable occurs when you access an item of a list by using parentheses. Parentheses are only applicable to callable objects like functions. To access elements in a list you have to use square brackets instead.
Error ‘list’ object is not callable with a List Comprehension
When working with list comprehensions you might have also seen the “object is not callable” error.
This is a potential scenario when this could happen.
I have created a list of lists variable called matrix and I want to double every number in the matrix.
>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [[2*row(index) for index in range(len(row))] for row in matrix]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "<stdin>", line 1, in <listcomp>
TypeError: 'list' object is not callable
This error is more difficult to spot when working with list comprehensions as opposed as when working with lists.
That’s because a list comprehension is written on a single line and includes multiple parentheses and square brackets.
If you look at the code closely you will notice that the issue is caused by the fact that in row(index) we are using parentheses instead of square brackets.
This is the correct code:
>>> [[2*row[index] for index in range(len(row))] for row in matrix]
[[2, 4, 6], [8, 10, 12], [14, 16, 18]]
Conclusion
Now that we went through few scenarios in which the errorobject is not callable can occur you should be able to fix it quickly if it occurs in your programs.
I hope this article has helped you save some time! 🙂
Related posts:

I’m a Tech Lead, Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!
follow Welcome to Flask! Learning Flask, here we go Reconstructing User Model When running the script, an error is reported:
TypeError: ‘bool’ object is not callable
This is the user model:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
nickname = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
posts = db.relationship('Post', backref='author', lazy='dynamic')
@property
def is_authenticated(self):
return True
@property
def is_active(self):
return True
@property
def is_anonymous(self):
return False
def get_id(self):
try:
return unicode(self.id)
This is the code at the time of the call:
from flask import render_template, flash, redirect, session, url_for, request, g
from flask.ext.login import login_user, logout_user, current_user, login_required
from app import app, db, lm, oid
from .forms import LoginForm
from .models import User
@app.route('/login', methods=['GET', 'POST'])
@oid.loginhandler
def login():
if g.user is not None and g.user.is_authenticated():
Solution:
According to the references:
Is u authenticated is a property, not a method. Just remove the brackets. There are two printing errors in this section of the book. Please refer to the git source code.
Put the wrong place:
if g.user is not None and g.user.is_authenticated():
Modified to
if g.user is not None and g.user.is_authenticated:
Then you don’t make a mistake.