-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass & Objects.py
More file actions
155 lines (121 loc) · 3.74 KB
/
Class & Objects.py
File metadata and controls
155 lines (121 loc) · 3.74 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# WHAT IS CLASS ?
'''
- A class is like an object constructor or a blueprint for creating a object.
- A python class is a group of attributes and methods. Attributes are represented by variable that contains data and
Method that performs an action or task. Methos is similar to function.
- To creating a class we use keyword "class:"
- Syntax: class <Class_name>:
'''
# EXAMPLE:
'''
class MyFirstClass:
cls = "Uber Auto" # CLASS VARIABLE
'''
# WHAT IS OBJECT ?
'''
- Object represents the base class name from where all classes in python are derived.
- This class is also derived from object class.
- Objects are optional.
- To create object we use class
- Syntax: object_name = Class_name()
'''
# EXAMPLE:
'''
obj = MyFirstClass()
print(obj.cls)
'''
# __init__() function
'''
- It is a built-in function and we call this as a constructor.
- All classes have a function called __init__(), which is always executed when the class is being initiated.
- Use the __init__() function to assign values to object properties or other operations that are necessary to do when
the object is being created.
'''
# EXAMPLE:
'''
class FamilyMan:
def __init__(self, name, age): # self -> self is a variable which refers to current class instance/object.
self.naam = name # INSTANCE VARIABLE
self.umar = age # INSTANCE VARIABLE
FM = FamilyMan('Nitin', 26)
print(FM.naam)
print(FM.umar)
'''
# OBJECT METHOD
'''
- Object can also contain methods. Methods in object are functon that belongs to the object.
- Syntax: objectname.Method_name
'''
# EXAMPLE:
'''
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def MyFun(self): # CLASS/INSTANCE METHOD
print(self.name)
print(self.age)
Obj = Person('Neha', 25)
Obj.MyFun()
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
# EXAMPLE: 1
'''
class MyClass:
x = 25
obj = MyClass()
print(obj.x)
'''
# EXAMPLE: 2
'''
class Person:
def __init__(self, Name, Age):
self.your_name = Name
self.your_age = Age
obj1 = Person('Neha', 27)
obj2 = Person('Ruchita', 30)
print(obj1.your_name, obj1.your_age)
print(obj2.your_name, obj2.your_age)
'''
# EXAMPLE: 3
'''
class Mobile:
Android = "Nothing Phone 1"
IOS = "I Phone 15"
def favourite(self):
print("Budget friendly: ", self.Android)
print("Premium: ", self.IOS)
choice = Mobile()
choice.favourite()
'''
# EXAMPLE: 4
'''
class Employee:
def __init__(self, Name, Age, City):
self.Emp_Name = Name
self.Emp_Age = Age
self.Emp_City = City
def show(self):
print("New employee name:", self.Emp_Name)
print("His age:", self.Emp_Age)
print("His current working location:", self.Emp_City)
obj = Employee('Nitin', 26, 'Banglore')
obj.show()
'''
# EXAMPLE: 5
'''
class Dog:
animal = 'kutta'
def __init__(self, breed):
self.breed = breed
def setcolor(self, color):
self.color = color
def getcolor(self):
return self.color
obj = Dog("Pug")
obj.setcolor("brown")
print(obj.getcolor())
print(obj.breed)
print(obj.animal)
'''
# ☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻