-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29thOtherFunctionsofClassesinPython.py
89 lines (45 loc) · 1.95 KB
/
29thOtherFunctionsofClassesinPython.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# ! super() Function
# * How we used the Constructors of Parent class to spread info between 2 classes can be done in a more efficient way
class A:
def disp(self):
print("in disp A")
class B:
def disp(self):
print("in disp B")
class C(B, A): # * If the order is reversed, the print changes to '' in disp A '' (Also this is Multiple Inheritance)
def disp(self):
super().disp() # * This prints in disp B as B is the first Class to be inherited from
# * '' in disp A '' is not printed as B is first inherited class
print("in disp C")
c1 = C()
c1.disp()
# * Now super() use in defining variables across multiple classes
class Shape:
def __init__(self, name): # * Here self.name variable is assigned the value
self.name = name
def info(self): # * This function is not called as the info function in it's Child Class(which is Polygon) is called by the super() in the other children classes
return f"This is a {self.name}."
class Polygon(Shape):
def __init__(self, name, sides):
super().__init__(name)
self.sides = sides # * self.sides is assigned here
def info(self): # * This function is only called
return f"A {self.name} is a polygon with {self.sides} sides."
class Triangle(Polygon):
def __init__(self, name):
super().__init__(name, 3)
class Quadrilateral(Polygon):
def __init__(self, name):
super().__init__(name, 4)
trig = Triangle('Triangle')
print(trig.info())
rec = Quadrilateral('Rectangle')
print(rec.info())
# ! issubclass Function
# * Syntax: a = issubclass(childclassname, parentclassname)
# * This gives True if child class is actually a child of Parent Class else False
print(issubclass(Triangle, Shape)) # * True
# ! isinstance Function
# * Syntax: a = isinstance(object, classname)
# * This gives True if a variable is an object of a class else False
print(isinstance(trig, Triangle)) # * True