Chapter 12 Classes
12.3 Constructors¶
As introduced earlier, types integrate information, and the interfaces of a type include attributes, methods, constructors and operators. While attributes, methods and operators have been discussed in this book, constructors have not been formally defined. They refer to the mechanism to construct a new object, or to instantiate a type. For most built-in types, objects are constructed via literals. For example, the integer literal 123 leads to the construction of a new integer object, and the list literal [1, 2, 3] leads to the construction of a new list object. On the other hand, the construction of a new set object is done by calling set(). This is a constructor call, a function call that takes the name of a type as the function name, and returns a new object of the type. Constructor calls are general and applicable to custom types, although literals are applicable only to the most commonly used built-in types. Similar to other functions, constructors can take arguments. In fact, the type conversion functions, such as float(3) and complex(1, 2), can also regarded as constructor calls.
A constructor is a method, but is special in two ways. First, it is called automatically at the evaluation of a constructor call expression, when the class in instantiated. Second, it must be named __init__. All the arguments in a constructor call are passed into the corresponding constructor. For example, a constructor of the Account class can take three arguments, specifying the account holder name, the account number and the initial balance, respectively, assigning them to corresponding attributes of the new object.
In the example above, the constructor call Account(‘John’, 100001, 1000) leads to the constructor __init__ of the Account class being executed, with the new object being assigned to the self argument, ‘John’ being assigned to the name argument, 100001 being assigned to the number argument and 1000 being assigned to the amount argument.
Execution of the function body assigns the three arguments to three attributes of the self object, respectively. As a result, the new object a has the balance attribute once being constructed, and calling the method a.deposit(100) immediately after a’s construction does not lead to an attribute error. Note that the argument number and the attribute self.number share the name number, but are different variables.
The number of arguments in a constructor call must match the number of arguments in the constructor of the class. For the Account class, for example, the constructor takes 3 arguments in addition to self, and 3 arguments must be specified in a constructor call. On the other hand, default arguments, special list arguments and special dict arguments can also be used in constructors. For example, the default value of the argument balance can be made 0, so that it does not need to be specified in a constructor call.
In the example, the constructor call Account(‘Marina’, 100002) is simpler than the constructor call Account(‘John’, 100001, 1000) by using a default value for the third argument balance. The constructor can be further simplified by removing the argument number, and allocating account numbers automatically. In particular, a solution is to keep an overall account number record, with the starting account number being 100001. Each time a new account is created, the current overall account number is allocated to the new account, and then the overall record is increased by one. This solution can be achieved by using a class attribute for the total account number.
© Copyright 2024 GS Ng.