constructor
#### **`instance`**
```self
* Self is indispensable in the design specifications of python. It is possible to use a keyword other than "self" for the name (for example, myself), but it is customary to use self.
[Reference page](https://www.sejuku.net/blog/64106)
class Calc: def init(self, a): #constructor self.a = a
def add(self, b):
print(self.a + b)
def multiply(self, b):
print(self.a * b)
calc = Calc(10) calc.add(10) calc.multiply(10)
#### **`Execution result`**
```python
20
100
class Calc:
def __init__(self, a): #constructor
self.a = a
def add(self, b):
print(self.a + b)
def multiply(self, b):
print(self.a * b)
CalcExtends, a class that inherits from the Calc class
class CalcExtends(Calc): #Inherit Calc
def subtract(self, b):
print(self.a - b)
def divide(self, b):
print(self.a / b)
calc_extends = CalcExtends(3)
calc_extends.add(4)
calc_extends.multiply(4)
calc_extends.subtract(4)
calc_extends.divide(4)
Execution result
7
12
-1
0.75
Recommended Posts