AttributeError: 'float' object has no attribute 'X' (Python)

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# AttributeError: 'float' object has no attribute 'X' (Python)

The Python "AttributeError: 'float' object has no attribute" occurs when we try to access an attribute that doesn't exist on a floating-point number, e.g. 5.4.

To solve the error, make sure the value is of the expected type before accessing the attribute.

attributeerror float object has no attribute

# Calling split() on a floating-point number

Here is an example of how the error occurs.

main.py
example = 5.4 print(type(example)) # ๐Ÿ‘‰๏ธ <class 'float'> # โ›”๏ธ AttributeError: 'float' object has no attribute 'split' result = example.split('.') print(result)

float object has no attribute

We tried to call the split() method on a floating-point number and got the error.

If you print() the value you are accessing the attribute on, it will be a float.

To solve the error, you need to track down where exactly you are setting the value to a float in your code and correct the assignment.

To solve the error in the example, we have to convert the floating-point number to a string to be able to access the string-specific split() method.

main.py
example = 5.4 result = str(example).split('.') print(result) # ๐Ÿ‘‰๏ธ ['5', '4']

converting float to string before calling method

# Calling round() on a floating-point number

Another common cause of the error is calling a round() method on a float.

main.py
example = 3.6 # โ›”๏ธ AttributeError: 'float' object has no attribute 'round' result = example.round()

attributeerror float object has no attribute round

To solve the error, we have to pass the float as an argument to the round() function, not call the function on the float.

main.py
example = 5.4 result = round(example) print(result) # ๐Ÿ‘‰๏ธ 5

pass float as argument to round function

Instead of accessing the round() function on the float, e.g. example.round(), we have to pass the floating point number as an argument to the round() function.

The round function takes the following 2 parameters:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe number of digits after the decimal the number should have after the operation (optional)

The round function returns the number rounded to ndigits precision after the decimal point.

If ndigits is omitted, the function returns the nearest integer.

Instead of accessing the round() function on the float, e.g.my_float.round(), we have to pass the floating point number as an argument to the round() function, e.g. round(3.5).

# Check if an object contains an attribute

If you need to check whether an object contains an attribute, use the hasattr function.

main.py
example = 5.4 if hasattr(example, 'round'): print(example.round) else: print('Attribute is not present on object') # ๐Ÿ‘‰๏ธ This runs

check if object contains attribute

The hasattr() function takes the following 2 parameters:

NameDescription
objectThe object we want to test for the existence of the attribute
nameThe name of the attribute to check for in the object

The hasattr() function returns True if the string is the name of one of the object's attributes, otherwise False is returned.

Using the hasattr function would handle the error if the attribute doesn't exist on the object, however, you still have to figure out where the variable gets assigned a float value in your code.

# Figure out where the variable got assigned a floating-point number

A good way to start debugging is to print(dir(your_object)) and see what attributes the object has.

Here is an example of what printing the attributes of a float looks like.

main.py
example = 5.4 # [..., 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real', ...] print(dir(example))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you will get the error.

To solve the error, either convert the value to the correct type before accessing the attribute, or correct the type of the value you are assigning to the variable before accessing any attributes.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev