Python’s __int__() and __float__() Methods

Today we’re going to talk about two of Python’s most underrated methods `__int__()` and `__float__()`. These guys are often overlooked in favor of their more popular cousins like `__str__()` and `__len__()`, but they deserve some love too!

So, what do these methods actually do? Well, let’s start with the basics. When you call a method on an object in Python, it executes whatever code is inside that method. For example:

# This script defines a class called MyClass
class MyClass:
    # This is the constructor method, which is called when an instance of the class is created
    def __init__(self):
        # This line initializes an attribute called x with a value of 5 for each instance of the class
        self.x = 5
        
    # This is a method called my_method, which can be called on an instance of the class
    def my_method(self):
        # This line prints the string "Hello, world!" when the method is called
        print("Hello, world!")
        
# This line creates an instance of the MyClass class and assigns it to the variable my_obj
my_obj = MyClass()
# This line calls the my_method method on the my_obj instance, which prints "Hello, world!"
my_obj.my_method() # Outputs "Hello, world!"

In this example, we’re defining a class called `MyClass`, which has an initializer (`__init__`) and a method called `my_method`. When we create an object of type `MyClass` (called `my_obj`), we can call the `my_method()` on it.

Now, our two heroes `__int__()` and `__float__()`. These methods are called when you try to convert a Python object to an integer or float respectively. For example:

# Creating a class called MyClass
class MyClass:
    # Defining a constructor method called __init__ that takes in the parameter self
    def __init__(self):
        # Setting the attribute x to the string "5"
        self.x = "5"
        
    # Defining a method called __int__ that takes in the parameter self
    def __int__(self):
        # Returning the integer 10
        return 10
        
# Creating an object of type MyClass and assigning it to the variable my_obj
my_obj = MyClass()
# Calling the __int__ method on my_obj and converting the returned value to an integer using the int() function
print(int(my_obj)) # Outputs 10, not an error!

# The __int__ method is called when trying to convert a Python object to an integer
# In this case, it returns the integer 10
# The int() function then converts this returned value to an integer and prints it out

In this example, we’re defining a class called `MyClass`, which has an initializer (`__init__`) and overrides the built-in `__int__()` method. When you try to convert our object to an integer using Python’s built-in `int()` function, it will call our custom implementation of `__int__()`.

This can be really useful if you have a class that represents some kind of data, but you want to be able to treat it as an integer or float in certain situations. For example:

# This script demonstrates how to use the __int__() method in a custom class to convert an object to an integer using Python's built-in int() function.

class MyClass:
    def __init__(self):
        self.x = 5 # Initializes the class with a variable x set to 5
        
    def __int__(self):
        return int(self.x) # Overrides the default __int__() method and returns the integer value of x
        
my_obj = MyClass() # Creates an instance of the MyClass class
print(int(my_obj)) # Calls the __int__() method and prints the integer value of x, which is 5

In this example, we’re defining a class called `MyClass`, which has an initializer (`__init__`) and overrides the built-in `__int__()` method. When you try to convert our object to an integer using Python’s built-in `int()` function, it will call our custom implementation of `__int__()`.

But wait what if we want to override both `__int__()` and `__float__()`? Can we do that too? You betcha! Here’s an example:

# This script demonstrates how to override the built-in __int__() and __float__() methods in Python.

class MyClass:
    def __init__(self):
        self.x = "5" # Initializes the class with a string value of "5"

    def __int__(self):
        return int(self.x) # Overrides the built-in __int__() method and converts the string value to an integer using the int() function

    def __float__(self):
        return float(self.x) # Overrides the built-in __float__() method and converts the string value to a float using the float() function

my_obj = MyClass() # Creates an instance of the MyClass object
print(int(my_obj)) # Calls the overridden __int__() method and outputs the integer value of 5
print(float(my_obj)) # Calls the overridden __float__() method and outputs the float value of 5.0

In this example, we’re defining a class called `MyClass`, which has an initializer (`__init__`) and overrides both the built-in `__int__()` method and the built-in `__float__()` method. When you try to convert our object to either an integer or float using Python’s built-in functions, it will call our custom implementations of those methods.

Remember , `__int__()` and `__float__()` are your friends. They can help you convert objects to integers or floats in ways that might not be immediately obvious.

SICORPS