Chapter 12 Classes
12.4 Class Attributes¶
A class attribute can be defined by writing an assignment statement in the indented statement block in a class statement. For example:
>>> class A:
... a=1
...
>>> A.a
1
In this example, a is an attribute to the class A, and can be accessed by the expression A.a. The use of the dot operator in A.a is similar to the use of the dot operator in the expression Account.deposit(a, 100), which accesses a method of a class. Recall that the def statement constructs a new function object, and assigns it to the function name identifier specified in the def line. As a result, it can be regarded as a special assignment statement, with the object being assigned being a function object. In this perspective, the definition of class attributes and methods are consistent, both being assignments within a class statement.
Now back to the Account type. The overall account number can be assigned to the class Account as a class attribute:
As the example above shows, new instances of the Account class are given account numbers automatically, starting from 100001. This is achieved by assigning the current value of Account.total to the new object, and increasing Account.total in the constructor.
In memory, the class statement defines a new class object, and assigns it to the class name identifier in the class line. This is similar to the def statement, which defines a function object in memory and gives it a name. However, unlike the def statement, the execution of which does not invoke the execution of the indented statement block (i.e. function body), the execution of the class statement leads to the execution of the indented statement block under the class line. During the execution of the statement block, each time a new identifier is defined, it is taken as a class attribute or method. Similarly, each time a def statement is executed, the function is taken as a method.
The example above consists of 5 statements, the first being a class statement, the second and third being two assignment statements, with the right hand side of = being constructor call expressions, and the forth and fifth being two getattr expressions. When the first statement is executed, a class object is constructed and bound to the identifier Account in the global binding table. The indented statement block under the class line is executed, with total being taken as an attribute of Account, and __init__ and deposit as two methods.
When the second statement, a = Account(‘John’) is executed, the constructor Account.__init__ is called automatically, with the argument self being assigned the new object, the argument name being assigned the string ‘John’, and the argument balance being assigned 0 by default. Execution of the method body assigns name to the attribute self.name, Account.total, which is currently 100001, to the attribute self.number, and balance to the attribute self.balance. Then the value of Account.total is increased from 100001 to 100002 by the execution of Account.total += 1. The execution of the third statement, b = Account(‘Marina’), follows the same procedure, except that the argument name is assigned ‘Marina’, and the current value of Account.total is 100002.
© Copyright 2024 GS Ng.