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

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

$
0
0

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

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

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

Let us take a simple example to reproduce this error.

# Method return float instead of dict
def fetch_data():
    output = 44.55
    return output


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

Output

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

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

Since we call the get() method on the float 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 float instead of dict
def fetch_data():
    output = "Toyota Car"
    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 'float'>

List of valid attributes in this object is  ['__abs__', '__add__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__set_format__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']

How to fix AttributeError: ‘float’ 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 float 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": "NordVPN",  "Price": "22.22"}
    return output


data = fetch_data()

# Get the Price
print(data.get("Price"))

Output

22.22

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": "NordVPN",  "Price": "22.22"}
    return output


data = fetch_data()

# Get the Price
if (type(data) == dict):
    print(data.get("Price"))
else:
    print("The object is not dictionary and it is of type ", type(data))

Output

22.22

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": "NordVPN",  "Price": "22.22"}
    return output


data = fetch_data()

# Get the Price
if (hasattr(data, 'get')):
    print(data.get("Price"))
else:
    print("The object does not have get attribute")

Output

22.22

Conclusion

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

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