Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Providing Multiple Constructors in Your Python Classes (Summary)

Writing Python classes with multiple constructors can make your code more versatile and flexible, covering a wide range of use cases. Multiple constructors are a powerful feature that allows you to build instances of the underlying class using arguments of different types, a different number of arguments, or both, depending on your needs.

In this video course, you learned how to:

  • Simulate multiple constructors using optional arguments and type checking
  • Write multiple constructors using the built-in @classmethod decorator
  • Overload your class constructors using the @singledispatchmethod decorator

You also learned how Python internally constructs instances of a given class and how some standard-library classes provide multiple constructors.

With this knowledge, now you can spice up your classes with multiple constructors, equipping them with several ways to tackle the instantiation process in Python.

To learn more about the concepts you’ve covered in this course, check out:

Download

Sample Code (.zip)

2.0 KB
Download

Course Slides (.pdf)

5.9 MB

Become a Member to join the conversation.