ZetCode

Python __float__ Method

Last modified April 8, 2025

This comprehensive guide explores Python's __float__ method, the special method responsible for converting objects to floating-point numbers.

Basic Definitions

The __float__ method is a special method that defines how an object should be converted to a float. It is called by the built-in float() function when converting an object.

Key characteristics: it must return a floating-point number, is automatically invoked during float conversion, and enables custom objects to support float operations. It's part of Python's numeric protocol.

Basic __float__ Implementation

Here's a simple implementation showing how __float__ works with a custom class. The method returns the float representation of the object.

basic_float.py
class Temperature:
    def __init__(self, celsius):
        self.celsius = celsius
    
    def __float__(self):
        return float(self.celsius)

temp = Temperature(23.5)
print(float(temp))  # 23.5

This example shows a Temperature class that stores temperature in Celsius. The __float__ method returns the stored value as a float.

When float(temp) is called, Python automatically invokes temp.__float__() to get the float representation. This enables custom objects to work with float operations.

Converting Custom Objects to Float

__float__ allows custom objects to define their own conversion logic to floating-point numbers, enabling integration with numeric operations.

custom_conversion.py
class Fraction:
    def __init__(self, numerator, denominator):
        self.numerator = numerator
        self.denominator = denominator
    
    def __float__(self):
        return self.numerator / self.denominator

half = Fraction(1, 2)
print(float(half))  # 0.5
quarter = Fraction(1, 4)
print(float(quarter) * 100)  # 25.0

This Fraction class implements __float__ to return the decimal value of the fraction. The conversion enables float operations like multiplication.

The method performs the division when converting to float, providing the most natural float representation for a fraction object.

Handling Non-Numeric Values

__float__ can include logic to handle cases where conversion to float isn't straightforward or requires special processing.

non_numeric.py
class Measurement:
    def __init__(self, value, unit):
        self.value = value
        self.unit = unit
    
    def __float__(self):
        if self.unit == 'km':
            return float(self.value * 1000)
        elif self.unit == 'cm':
            return float(self.value / 100)
        return float(self.value)

dist1 = Measurement(5, 'km')
dist2 = Measurement(150, 'cm')
print(float(dist1))  # 5000.0
print(float(dist2))  # 1.5

This Measurement class converts different units to meters when converting to float. The __float__ method handles the unit conversion logic.

The example demonstrates how __float__ can encapsulate conversion rules, making the object more flexible when used in numeric contexts.

Error Handling in __float__

The __float__ method should handle cases where conversion isn't possible by raising appropriate exceptions, similar to built-in types.

error_handling.py
class Percentage:
    def __init__(self, value):
        self.value = value
    
    def __float__(self):
        try:
            return float(self.value.strip('%')) / 100
        except AttributeError:
            return float(self.value) / 100
        except (ValueError, TypeError):
            raise ValueError(f"Cannot convert {self.value} to percentage")

p1 = Percentage('50%')
p2 = Percentage(0.75)
print(float(p1))  # 0.5
print(float(p2))  # 0.75
# p3 = Percentage('invalid')
# float(p3)  # Raises ValueError

This Percentage class handles string percentages (with '%' sign) and numeric values. Invalid values raise ValueError, matching Python's convention.

The error handling makes the conversion robust while maintaining clear error messages when conversion fails, similar to built-in types.

Combining __float__ with Other Numeric Methods

__float__ often works with other numeric special methods to provide complete numeric behavior for custom objects.

numeric_methods.py
class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
    def __float__(self):
        return (self.x**2 + self.y**2)**0.5
    
    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)
    
    def __repr__(self):
        return f"Vector({self.x}, {self.y})"

v1 = Vector(3, 4)
v2 = Vector(6, 8)
print(float(v1))  # 5.0 (magnitude)
print(float(v1 + v2))  # 15.0

This Vector class implements __float__ to return the vector's magnitude, while also supporting vector addition through __add__.

The example shows how __float__ can provide a meaningful numeric representation while other methods handle different operations, creating a complete numeric type.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.