Last modified: Dec 12, 2025 By Alexander Williams
Fix AttributeError: 'int' object has no attribute 'update'
Python errors can be confusing. The AttributeError is common. It means you tried to use a method on the wrong object type. This guide explains the 'int' object has no attribute 'update' error.
We will show you why it happens. You will learn how to fix it. We will also teach you how to avoid it in the future.
Understanding the AttributeError
An AttributeError occurs in Python. It happens when you try to access an attribute. Or when you try to call a method. But the object does not support that operation.
The error message is very specific. It tells you the object type. Here, it is an 'int'. It also tells you the missing attribute. Here, it is 'update'.
The update method belongs to dictionaries. It is used to merge one dictionary into another. An integer is a number. It does not have an update method.
Why This Error Happens
The core cause is a type confusion. Your code expects a dictionary. But the variable holds an integer instead. This is a common bug.
It often happens when a function returns different types. Or when variable values change during program flow. You might think a variable is a dict. But it is actually an int.
Let's look at a simple example that causes the error.
# Example causing the error
my_variable = 42 # This is an integer
my_variable.update({"key": "value"}) # Trying to use dict method on int
Traceback (most recent call last):
File "", line 2, in
AttributeError: 'int' object has no attribute 'update'
The error is clear. You cannot call update on the number 42.
Common Scenarios and Fixes
Let's explore real situations. You will see how this error creeps in. We will provide fixes for each case.
Scenario 1: Function Returns Unexpected Type
A function might return an integer under certain conditions. Your code might assume it always returns a dictionary.
def get_data(id):
if id < 0:
return -1 # Error code as integer
else:
return {"name": "Alice", "score": 95} # Normal dict return
result = get_data(-5)
result.update({"status": "inactive"}) # ERROR if result is -1
Fix: Check the type before calling the method. Use isinstance() or check the value.
result = get_data(-5)
if isinstance(result, dict):
result.update({"status": "inactive"})
else:
print(f"Error: Expected dict, got {type(result).__name__}")
Scenario 2: Variable Reassignment
You might reassign a variable. It was a dictionary earlier. Later, it becomes an integer. This breaks subsequent code.
config = {"theme": "dark"} # Starts as a dict
# ... many lines of code ...
config = 1 # Oops, accidentally reassigned to an int
# ... more code ...
config.update({"language": "en"}) # ERROR
Fix: Be careful with variable names. Avoid reusing names for different types. Use meaningful names.
config_dict = {"theme": "dark"}
# Use config_dict consistently
counter = 1 # Separate variable for integer
config_dict.update({"language": "en"}) # Works fine
Scenario 3: Confusion with Similar Errors
This error is part of a family. You might see it with other types. Like 'str' object has no attribute 'update'.
The root cause is the same. You are calling a dictionary method on a non-dict object. For example, trying to update a string or a list.
You can learn more about related errors. See our guide on Fix AttributeError: 'str' object has no attribute 'update'.
Another common mix-up is using list methods on dicts. Read about Fix Python AttributeError 'dict' object has no attribute 'append'.
How to Debug and Prevent the Error
Debugging is key. Use print() or a debugger. Check the type of your variable before the error line.
print(f"Variable is: {my_variable}")
print(f"Type is: {type(my_variable)}")
# Now you know if it's an int or dict
Prevention is better than cure. Follow these tips.
Use type hints. They make your code's intent clear. They help tools catch errors early.
from typing import Union
def get_data(id: int) -> Union[dict, int]:
# Return type is clearly documented
pass
Write unit tests. Tests should check function return types. They ensure your code handles all cases.
Initialize variables properly. If a variable should be a dict, start it as an empty dict. Not as None or 0.
Correct Use of the Update Method
The update method is for dictionaries. It merges keys and values from another dict. Or from an iterable of key-value pairs.
Here is the correct usage.
user = {"name": "Bob"}
new_info = {"age": 30, "city": "London"}
user.update(new_info)
print(user)
{'name': 'Bob', 'age': 30, 'city': 'London'}
Remember, the object must be a dictionary. If you get this error, your object is not a dict. It is likely an integer.
Similar confusion happens with other methods. For instance, trying to use items on an integer. Learn more in Fix Python AttributeError: 'int' object has no attribute 'items'.
Conclusion
The AttributeError 'int' object has no attribute 'update' is straightforward. You tried to use a dictionary method on an integer.
To fix it, find where the variable became an integer. Check function returns. Watch for variable reassignment. Use type() or isinstance() to verify types.
Always ensure you are calling methods on the correct data type. Understanding Python's data structures prevents many errors. Keep your code clean and well-tested.
This error teaches a valuable lesson. Python is dynamically typed. But you must still keep track of your variable types. Happy coding!