Python __float__ Method

Complete guide to Python's __float__ method covering numeric conversion, custom objects, and practical examples.

Python __float__ Method

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

  • Return a float: Always return a floating-point number

  • Handle errors: Raise appropriate exceptions for invalid conversions

  • Keep it simple: Focus on conversion logic only

  • Document behavior: Clearly document what the conversion represents

  • Consider rounding: Handle floating-point precision appropriately

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.

ad ad