Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Providing Multiple Constructors in Your Python Classes (Overview)

Sometimes you need to write a Python class that provides multiple ways to construct objects. In other words, you want a class that implements multiple constructors. This kind of class comes in handy when you need to create instances using different types or numbers of arguments. Having the tools to provide multiple constructors will help you write flexible classes that can adapt to changing needs.

Python offers several techniques and tools that you can use to construct classes, including simulating multiple constructors through optional arguments, customizing instance creation via class methods, and doing special dispatch with decorators. If you want to learn about these techniques and tools, then this video course is for you.

In this video course, you’ll learn how to:

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

You’ll also get a peek under the hood at how Python internally constructs instances of a regular class and how some standard-library classes provide multiple constructors.

To get the most out of this video course, you should have basic knowledge of object-oriented programming and understand how to define class methods with @classmethod. You should also have experience working with decorators in Python.

Download

Sample Code (.zip)

2.0 KB

Download

Course Slides (.pdf)

5.9 MB

00:00 Providing Multiple Constructors in Your Python Classes.

00:06 Sometimes you need to write a Python class that provides multiple ways to construct objects. In other words, you want a class that implements multiple constructors.

00:15 This kind of class comes in handy when you need to create instances using different types or numbers of arguments. Having the tools to provide multiple constructors will help you write flexible classes that can adapt to changing needs. In Python, there are several techniques and tools that you can use to construct classes, including simulating multiple constructors through optional arguments, customizing instance creation via class methods, and doing special dispatch with decorators.

00:46 If you want to learn about these techniques and tools, then this course is for you. In this course, you’ll learn how to simulate multiple constructors with optional arguments and type checking, write multiple constructors using the built-in @classmethod decorator, and overload your class constructors using the @singledispatchmethod decorator.

01:07 You’ll also get a look under the hood at how Python internally constructs instances of a regular class and how some standard-library classes provide multiple constructors. To get the most out of this course, you should have basic knowledge of object-oriented programming and understand how to define class methods with the @classmethod decorator.

01:27 You should also have experience working with decorators in Python. As ever, Real Python has you covered if you need to learn more about these subjects, with this course covering object-oriented programming, this one covering class methods, and this one for decorators.

01:48 In this course, any code you see running in the REPL will be using the bpython REPL. It offers a number of improvements over the standard REPL, such as color-coding, but all of the code you see will run in the standard Python REPL, which is typically launched by typing python in a terminal window.

02:05 So now you know what’s going to be covered in the course, let’s get started.

Become a Member to join the conversation.