[Solved] AttributeError: ‘int’ object has no attribute ‘get’

The AttributeError: ‘int’ object has no attribute ‘get’ mainly occurs when you try to call the get() method on the integer type. The attribute get() method is present in the dictionary and must be called on the dictionary data type.

In this tutorial, we will look at what exactly is AttributeError: ‘int’ object has no attribute ‘get’ and how to resolve this error with examples.

What is AttributeError: ‘int’ object has no attribute ‘get’?

If we call the get() method on the integer data type, Python will raise an AttributeError: ‘int’ object has no attribute ‘get’. The error can also happen if you have a method which returns an integer instead of a dictionary.

Let us take a simple example to reproduce this error.

# Method return integer instead of dict
def fetch_data():
    output = 100
    return output


data = fetch_data()
print(data.get("name"))

Output

AttributeError: 'int' object has no attribute 'get'

In the above example, we have a method fetch_data() which returns an integer instead of a dictionary.

Since we call the get() method on the integer type, we get AttributeError.

We can also check if the variable type using the type() method, and using the dir() method, we can also print the list of all the attributes of a given object.

# Method return integer instead of dict
def fetch_data():
    output = 100
    return output


data = fetch_data()
print("The type of the object is ", type(data))
print("List of valid attributes in this object is ", dir(data))

Output

The type of the object is  <class 'int'>

List of valid attributes in this object is  ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

How to fix AttributeError: ‘int’ object has no attribute ‘get’?

Let us see how we can resolve the error.

Solution 1 – Call the get() method on valid dictionary

We can resolve the error by calling the get() method on the valid dictionary object instead of the integer type.

The dict.get() method returns the value of the given key. The get() method will not throw KeyError if the key is not present; instead, we get the None value or the default value that we pass in the get() method.

# Method returns dict
def fetch_data():
    output = {"Name": "Audi", "Release_Date": "2022", "Price": "$45000"}
    return output

data = fetch_data()

# Get the price of the car
print(data.get("Price"))

Output

$45000

Solution 2 – Check if the object is of type dictionary using type

Another way is to check if the object is of type dictionary; we can do that using the type() method. This way, we can check if the object is of the correct data type before calling the get() method.

# Method returns dict
def fetch_data():
    output = {"Name": "Audi", "Release_Date": "2022", "Price": "$45000"}
    return output


data = fetch_data()

# Check if the object is dict
if (type(data) == dict):
    print(data.get("Price"))

Output

$45000

Solution 3 – Check if the object has get attribute using hasattr

Before calling the get() method, we can also check if the object has a certain attribute. Even if we call an external API which returns different data, using the hasattr() method, we can check if the object has an attribute with the given name.

# Method returns dict
def fetch_data():
    output = {"Name": "Audi", "Release_Date": "2022", "Price": "$45000"}
    return output


data = fetch_data()

# Check if the object has get attribute
if (hasattr(data, 'get')):
    print(data.get("Price"))

Output

$45000

Conclusion

The AttributeError: ‘int’ object has no attribute ‘get’ occurs when you try to call the get() method on the integer type. The error also occurs if the calling method returns an integer instead of a dictionary object.

We can resolve the error by calling the get() method on the dictionary object instead of an integer. We can check if the object is of type dictionary using the type() method, and also, we can check if the object has a valid get attribute using hasattr() before performing the get operation.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Lstrip()

Python String lstrip()

Table of Contents Hide lstrip() Syntaxlstrip() Parameterslstrip() Return ValueExample 1: Working of lstrip()Example 2 – How to use lstrip() method in real world? The Python String lstrip() method is a…
View Post
Remove Character From String Python

Remove Character From String Python

Table of Contents Hide Python Remove a Character from a StringPython Remove Character from String using replace()Remove a single character from a stringRemove all occurrences of a character from a…
View Post
Python List Remove()

Python List remove()

Table of Contents Hide Syntax of List remove() remove() ParametersReturn Value from List remove()Example 1: Remove element from the listExample 2: remove() method when list has duplicate itemsExample 3: Deleting element…
View Post
Python Trim String Strip() Vs Rstrip() Vs Lstrip()

Python Trim String – rstrip(), lstrip(), strip()

Table of Contents Hide Difference between strip() vs rstrip() vs lstrip()strip()Example of strip() function  rstrip() Example of rstrip() function   lstrip() Example of lstrip() function   Python provides three methods to trim the…
View Post