Creating Custom Type Conversion in Python

In Python, you can create custom type conversion methods for your own classes using the __int__, __float__, __str__, etc. methods. These methods allow you to define how your class instances should be converted to other types.

Defining Custom Type Conversion Methods

To define a custom type conversion method, you need to implement one of the following methods in your class:

__int__: Converts the instance to an integer.

__float__: Converts the instance to a float.

__str__: Converts the instance to a string.

__repr__: Returns a string representation of the instance, often used for debugging purposes.

__bool__: Converts the instance to a boolean value.

Example:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return f"Point({self.x}, {self.y})"

    def __int__(self):
        return int(self.x + self.y)

    def __float__(self):
        return float(self.x + self.y)

p = Point(3, 4)

print(str(p))  # Output: Point(3, 4)
print(int(p))  # Output: 7
print(float(p))  # Output: 7.0

In this example, we’ve defined three custom type conversion methods:

  • __str__: Returns a string representation of the Point instance, including the x and y coordinates.
  • __int__: Converts the Point instance to an integer, which is the sum of the x and y coordinates.
  • __float__: Converts the Point instance to a float, which is the sum of the x and y coordinates as a floating-point number.

Using Custom Type Conversion

You can use custom type conversion methods in various ways:

  • String Representation: Use the str() function to get the custom string representation of your class instance.
  • Integer Conversion: Use the int() function to convert your class instance to an integer.
  • Float Conversion: Use the float() function to convert your class instance to a float.
  • Boolean Conversion: Use the bool() function to convert your class instance to a boolean value.

Example:

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def __str__(self):
        return f"Rectangle({self.width}, {self.height})"

    def __int__(self):
        return int(self.width * self.height)

    def __float__(self):
        return float(self.width * self.height)

    def __bool__(self):
        return self.width > 0 and self.height > 0

r = Rectangle(5, 10)

print(str(r))  # Output: Rectangle(5, 10)
print(int(r))  # Output: 50
print(float(r))  # Output: 50.0
print(bool(r))  # Output: True

In this example, we’ve defined four custom type conversion methods for the Rectangle class:

  • __str__: Returns a string representation of the Rectangle instance, including the width and height.
  • __int__: Converts the Rectangle instance to an integer, which is the area of the rectangle.
  • __float__: Converts the Rectangle instance to a float, which is the area of the rectangle as a floating-point number.
  • __bool__: Converts the Rectangle instance to a boolean value, which is True if both the width and height are greater than zero.