Simulating Multiple Constructors
00:00 Simulating Multiple Class Constructors.
00:05
A pretty useful technique for simulating multiple constructors in a Python class is to provide .__init__()
with optional arguments using default argument values. This way, you can call the class constructor in different ways and get a different behavior each time.
00:20
Another strategy is to check the data type of the arguments to .__init__()
to provide different behaviors depending on the concrete data type that you pass in the call.
00:30
This technique allows you to simulate multiple constructors in a class. You’ll learn the basics of how to simulate multiple ways to construct objects by providing appropriate default values for the arguments of the .__init__()
method and also by checking the data type of the arguments to this method.
00:48
Both approaches require only one implementation of .__init__()
, and in the next section, you’ll look at how to use optional argument values to achieve this technique.
Become a Member to join the conversation.