-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAbstract_Base_Classes(ABC).py
54 lines (37 loc) · 1.6 KB
/
Abstract_Base_Classes(ABC).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'''Duck typing is useful as it simplifies the code and the user can implement the functions without worrying about the data type.
But this may not be the case all the time.
The user might not follow the instructions to implement the necessary steps for duck typing.
To cater to this issue, Python introduced the concept of abstract base classes, or ABC'''
'''
Def: Abstract Base Classes define a set of methods and properties that a class must implement in order to be considered
a duck type instance of that class
'''
# Syantax
'''
To define Abstract class we use abc module. The Abstract base class is inherited from the Built-in ABC class.
We have to use the decorator @abstractmethod above the method that we want to declare as an abstract method.
from abc import ABC, abstractmethod
class ParentClass(ABC):
@abstractmethod
def method(self)
Note: Methods with @abstractmethod decorators must be defined in the child class.
Abstract methods must be defined in child classes for proper implementation of inheritance.
'''
from abc import ABC, abstractmethod
class Shape(ABC): # Shape is a child class of ABC
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Square(Shape):
def __init__(self, length):
self.length = length
def area(self):
return (self.length * self.length)
def perimeter(self):
return (4 * self.length)
square = Square(4)
# this code will not generate an error since abastract methods have been
# defined in the child class, Square