Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

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

$
0
0

The AttributeError: ‘NoneType’ object has no attribute ‘get’ mainly occurs when you try to call the get() method on the None value. 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: ‘NoneType’ object has no attribute ‘get’ and how to resolve this error with examples.

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

If we call the get() method on the None value, Python will raise an AttributeError: ‘NoneType’ object has no attribute ‘get’. The error can also happen if you have a method which returns an None instead of a dictionary or if we forget the return statement in the function as shown below.

Let us take a simple example to reproduce this error.

# Method return None instead of dict
def fetch_data():
    output = {"name": "Audi",  "price": "$45000"}


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

Output

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

In the above example, we have a method fetch_data() which returns an None instead of a dictionary because the return statement is missing.

Since we call the get() method on the None value, 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 None instead of dict
def fetch_data():
    output = {"name": "Audi",  "price": "$45000"}


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 'NoneType'>

List of valid attributes in this object is  ['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
PS C:\Personal\IJS\Code>

How to fix AttributeError: ‘NoneType’ 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 None 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 return None instead of dict
def fetch_data():
    output = {"name": "Audi",  "price": "$45000"}
    return output


data = fetch_data()

# Get the  car Name
print(data.get("name"))

Output

Audi

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 return None instead of dict
def fetch_data():
    output = {"name": "Audi",  "price": "$45000"}
    return output


data = fetch_data()

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


softwares = None

if (type(softwares) == dict):
    print(softwares.get("name"))
else:
    print("The object is not dictionary and it is of type ", type(softwares))

Output

Audi
The object is not dictionary and it is of type  <class 'NoneType'>

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 return None instead of dict
def fetch_data():
    output = {"name": "Audi",  "price": "$45000"}
    return output


data = fetch_data()

# Check if the object is dict
if (hasattr(data, 'get')):
    print(data.get("name"))


# Incase of None value
softwares = None

if (hasattr(softwares, 'get')):
    print(softwares.get("name"))
else:
    print("The object does not have get attribute")

Output

Audi
The object does not have get attribute

Conclusion

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

We can resolve the error by calling the get() method on the dictionary object instead of an None. 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.


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>