-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance_method.py
More file actions
executable file
·100 lines (71 loc) · 2.69 KB
/
instance_method.py
File metadata and controls
executable file
·100 lines (71 loc) · 2.69 KB
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
90
91
92
93
94
95
96
97
98
99
100
class Payslips:
def __init__(self,name,payment,amount) -> None:
self.name= name
self.payment= payment #self.varriable =varriable or attribute
self.amount=amount
def pay(self):
self.payment ="yes"
def status(self):
if self.payment=="yes":
return self.name +" is paid "+ str(self.amount)
else:
return self.name + "is not paid yet"
#creating the instance of the class
nathan =Payslips("nathan","no",10000)
jp= Payslips("JP","yes",2000)
print(nathan.status(),"\n",jp.status()) #calling the fun with the instances
nathan.pay() #pay fun is made for the updating the value of the payment so,
print(nathan.status(),"\n",jp.status()) # status is the Method
print(jp.payment)
print(nathan.payment)
#Here the value of the pay is changed for the nathan not for jp , this is because
#Method inside the class is not affected by changing the one instance.
#Single Inheritence
class A():
pass
class B(A):
pass
#Multiple Inheritence
class A():
a = 1
class B():
b = 2
class C(A, B):
pass
c = C()
print(c.a, c.b)
# two classes called A and B are created and then variables a and b respectively are initialized with values.
# A new class C is then defined and classes A and B are passed to it.
# This is how multiple inheritance is done in Python.
# Multi Level Inheritance
class A():
a=1
class B(A):
b=2
class C(B):
pass
c = C()
print(c.a)
print(isinstance(c,A))
#there are three level of inheritance
# Above example of multi-level inheritance where the derived class C inherits from base class B.
#The class B is in turn a derived class of base class C. Class B here is an intermediary derived class.
#Built in Functions
#There are two built-in functions that can come in handy when trying to find
# the relationship between different classes and objects: issubclass() and isinstance().
issubclass(A,B)
print(issubclass(A,B)) # is A subclass of B
print(issubclass(C,A)) # is C subclass of A
#Super() function.
#The super() function is a built-in function that can be called inside the derived class and gives access to the methods and
#variables of the parent classes or sibling classes.
class Fruit():
def __init__(self,fruit) -> None:
print('Fruit type', fruit)
class FruitFlavour(Fruit):
def __init__(self) -> None:
super().__init__('apple') # changes inside the derived class
print("Appple is sweet")
apple=FruitFlavour()
#This happened because when you initialize the child class, you don’t initialize the base class with it.
# super() function helps you to achieve this and add the initialization of base class with the derived class.