Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Defining Multiple Constructors

00:00 Defining Multiple Class Constructors. Sometimes you’d like to write a class that allows you to construct objects using arguments of different data types or even a different number of arguments.

00:13 One way to achieve this is by providing multiple constructors in the class at hand. Each structure will allow you to create instances of the class using a different set of arguments.

00:25 Some programming languages, such as C++, C#, and Java, support what is known as function or method overloading. This feature allows you to provide multiple class constructors because it enables you to create multiple functions or methods with the same name and different implementations.

00:42 Method overloading means that depending on how you call the method at hand, the language will select the appropriate implementation to run. So, your method can perform different tasks according to the context of the call. Unfortunately, Python doesn’t support function overloading directly.

01:00 Python classes keep method names in an internal dictionary called .__dict__, which holds the class namespace. Like any Python dictionary, .__dict__ can’t have repeated keys, so you can’t have multiple methods with the same name in a given class.

01:16 If you try to do so, then Python will only remember the last implementation of the method at hand.

01:24 Here you can see an example demonstrating this.

01:35 In this example, you create Greeter as a Python class with two methods. Both methods have the same name, but they have slightly different implementations.

01:44 To learn what happens when two methods have the same name, save your class into a greet.py file in your working directory and run the following code in an interactive session.

02:02 In this example, you call .say_hello() on greeter, which is an instance of the Greeter class. You get Hello, Pythonista instead of Hello, World on your screen, which confirms that the second implementation of the method prevails over the first one.

02:18 This line of code inspects the contents of .__dict__ uncovering that the method’s name, say_hello, appears only once in the class namespace.

02:27 This is consistent with how dictionaries work in Python.

02:32 Something similar occurs with functions in a Python module and an interactive session. The last implementation of several functions with the same name prevails over the rest of the implementations. You define two functions with the same name, say_hello() in the same interpreter session.

02:49 As you can see, when you call the function, the second definition overwrites the first one, confirming that the last function definition prevails.

03:00 Another technique that some programming languages use to provide multiple ways to call a method or function is multiple dispatch. With this technique, you can write several different implementations of the same method or function and dynamically dispatch the desired implementation according to the type or other characteristics of the arguments that are used in the call.

03:20 You can use a couple of tools from the standard library to pull this technique into your Python code. Python is a fairly flexible and feature-rich language and provides a couple of ways to implement multiple constructors and make your classes more flexible.

03:35 In the next section, you’ll simulate multiple constructors by passing optional arguments and by checking the argument types to determine different behaviors in your instance initializers.

Become a Member to join the conversation.