Using Optional Arguments
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.
00:00
Using Optional Argument Values in .__init__()
.
00:05
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.
00:36
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.
00:58
Go ahead and create a power.py
file in your current directory. Then add the following code to implement CumulativePowerFactory
.
01:16
The initializer of CumulativePowerFactory
takes two optional arguments, exponent
and 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.
01:33
The star or asterisk symbol (*
) after 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 arg
to value
, you need to explicitly type arg=value
.
01:51
The 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.
02:13
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 exponent
.
02:31
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.
02:48
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 exponent
argument.
03:04
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.
03:26
This third example shows how CumulativePowerFactory
seems to have another constructor that allows you to create instances by providing the exponent
and start
arguments.
03:36
Now .total
starts with a value of 2205
, which initializes the sum of powers.
03:51
Using optional arguments when you’re implementing .__init__()
in your classes is a clean and Pythonic technique to create classes that simulate multiple constructors.
04:01
In the next section of the course, you’ll take a look at a different approach where you check the argument types to .__init__().
.
Become a Member to join the conversation.