To learn about providing optional arguments in your functions and methods using an undefined number of positional arguments or an undefined number of keyword arguments, check out Using Python Optional Arguments When Defining Functions.
Using Optional Arguments
Using Optional Argument Values in
An elegant and Pythonic way to simulate multiple constructors is to implement a
.__init__() method with optional arguments. You can do this by specifying appropriate default argument values.
00:17 You can also provide optional arguments in your functions and methods using an undefined number of positional arguments or an undefined number of keyword arguments.
00:27 Check out Using Python Optional Arguments When Defining Functions for more details on these options.
Let’s say you need to code a factory class called
CumulativePowerFactory. This class will create callable objects that compute specific powers, using a stream of numbers as input.
00:46 You also need your class to track the total sum of consecutive powers. Finally, your class should accept an argument holding an initial value for the sum of powers.
Go ahead and create a
power.py file in your current directory. Then add the following code to implement
The initializer of
CumulativePowerFactory takes two optional arguments,
start. The first argument holds the exponent that you’ll use to compute a series of powers. It defaults to
2, which is a commonly used value when it comes to computing powers.
The star or asterisk symbol (
exponent means that
start is a keyword-only argument. To pass a value to a keyword-only argument, you need to use the argument’s name explicitly. In other words, to set
value, you need to explicitly type
start argument holds the initial value to compute the cumulative sum of powers. It defaults to
0, which is the appropriate value for those cases in which you don’t have a previously computed value to initialize the cumulative sum.
The special method
.__call__() turns the instances of
CumulativePowerFactory into callable objects. In other words, you can call the instances of
CumulativePowerFactory like you call any regular function. Inside
.__call__(), you first compute the power of
base raised to
Then you add the resulting value to the current value of
.total. Finally, you return the computed power. To give this class a try, open a Python interactive session in a directory containing
power.py and run the code seen on-screen.
These examples show how
CumulativePowerFactory simulates multiple constructors. The first constructor doesn’t take arguments, and it allows you to create class instances that compute powers of
2, which is the default value of the
.total instance attribute holds the cumulative sum of computed powers as you go. The second example shows a constructor that takes
exponent as an argument and returns a callable instance that computes cubes. In this case,
.total works in the same way as the first example.
This third example shows how
CumulativePowerFactory seems to have another constructor that allows you to create instances by providing the
.total starts with a value of
2205, which initializes the sum of powers.
Using optional arguments when you’re implementing
.__init__() in your classes is a clean and Pythonic technique to create classes that simulate multiple constructors.
In the next section of the course, you’ll take a look at a different approach where you check the argument types to
Become a Member to join the conversation.