-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22classes_in_python.py
More file actions
140 lines (83 loc) · 2.93 KB
/
22classes_in_python.py
File metadata and controls
140 lines (83 loc) · 2.93 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#camelcase is followed for class ie BookName
#snakecase is followed for function name ie book_name
# class Book:
# pass
# obj=Book()
# print(type(obj))
# print(obj)
# print(Book)
# class Book:
# #attributes or properties section
# #these are class properties
# # title=""
# # author=""
# # price=0.0
# # discount=0.0
# class_variable="I am a class variable"
# def __init__(self,title,author,price,discount):
# #constructor is used to initialize the properties of the object
# # print("I am constructor of class Book:",self)
# self.title=title
# self.author=author
# self.price=price
# self.discount=discount
# #Behaviour or method sectin
# def calculate_sp(self):
# sp=self.price-(self.price*(self.discount)/100)
# return sp
# #Instantiating the class or making the object
# book1=Book("Python","Basanta",120,10)
# book2=Book("C++","Hari",100,20)
# #accessing objects properties
# print(book1.author)
# print(book1.price)
# print(book1.discount)
# print(book1.class_variable)
# print(book2.class_variable)
# print(book1.calculate_sp())
#methods
class student:
position="std"
def __init__(self,name,age,marks):
self.name=name
self.age=age
self.marks=marks
#instance method(can be called by instance or class but with an object)
def display(self):
print(self.name)
print(self.age)
print(self.marks)
print(self.position)
# @classmethod
# def display2(cls):
# print(cls.position)
# print(cls.name) #this will give error
#class method is used to create new object
# @classmethod
# def create_object(cls,name,age,marks):
# return cls(name,age,marks) #calls the class constructor
#static method
#cant access any of the class and object attributes
@staticmethod
def display3(a,b):
print(f"sum:{a+b}")
# s1=student("Basanta",20,90)
# s1.display()
# student.display(s1) ie.
# instance methods can be called by the class itself. However, when calling an instance method from the class, you would typically need to provide an instance of the class as an argument to the method or have a way to access an existing instance within the class.
#instance method can access both class and instance properties
#class method:> can be called by both class and obhect
#->has access to only class properties(object properties)
#->use decorator and first parameter is cls(for class object)
#class method
#calling class method
# student.display2()
#calling class method to create an object
# s2=student.create_object("Mukesh",40,24)
# print(s2.name)
# print(s2.age)
# print(s2.marks)
#static method
#calling
#can be called by both class and object
# student.display3(10,20)