Special Method Protocols

__float__() — numeric protocol

Provide a floating-point conversion; float() falls back to __index__ if this hook is absent. The hook must return the specified numeric type.

Formal Syntax & Structure

python syntaxpython
def __float__(self):

Return Value

float

Converted numeric value.

Working Example

Example Snippetpython
import operatorclass Number:    def __init__(self, value):        self.value = value    def __float__(self):        return float(self.value)print(float(Number(3)))
Python Reference