Categories
Python Answers

How to add function overloading with Python?

Spread the love

Sometimes, we want to add function overloading with Python.

In this article, we’ll look at how to add function overloading with Python.

How to add function overloading with Python?

To add function overloading with Python, we can replace overloading with making parameters optional.

For instance, we write

class Character(object):
    # ...

    def add_bullet(self, sprite=default, start=default, 
                 direction=default, speed=default, accel=default, 
                  curve=default):
        # ...

to add the add_bullet method that has the all the parameters made optional by setting their default value to default.

Then we can call add_bullet without passing in all the arguments.

Conclusion

To add function overloading with Python, we can replace overloading with making parameters optional.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *