You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.md
+194-1Lines changed: 194 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1044,4 +1044,197 @@ Data storage applications.
1044
1044
1045
1045
- What are security concerns related to IoT?
1046
1046
1047
-
- This is the common IoT Interview Questions asked in an interview. Data security and privacy are major concerns related to IoT. These devices are vulnerable to hacking and cloud endpoints could be used by hackers to attack servers. Software developers and device designers have to ensure adequate security and privacy measures.
1047
+
- This is the common IoT Interview Questions asked in an interview. Data security and privacy are major concerns related to IoT. These devices are vulnerable to hacking and cloud endpoints could be used by hackers to attack servers. Software developers and device designers have to ensure adequate security and privacy measures.
1048
+
1049
+
# Python Object Oriented Programming(OOP)
1050
+
1051
+
Python is a multi-paradigm programming language. Meaning, it supports different programming approach.
1052
+
1053
+
One of the popular approach to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).
1054
+
1055
+
An object has two characteristics:
1056
+
1057
+
attributes
1058
+
behavior
1059
+
Let's take an example:
1060
+
1061
+
Parrot is an object,
1062
+
1063
+
name, age, color are attributes
1064
+
singing, dancing are behavior
1065
+
The concept of OOP in Python focuses on creating reusable code. This concept is also known as DRY (Don't Repeat Yourself).
1066
+
1067
+
In Python, the concept of OOP follows some basic principles:
1068
+
1069
+
- Inheritance A process of using details from a new class without modifying existing class.
1070
+
- Encapsulation Hiding the private details of a class from other objects.
1071
+
- Polymorphism A concept of using common operation in different ways for different data input.
1072
+
1073
+
- Class
1074
+
A class is a blueprint for the object.
1075
+
1076
+
We can think of class as an sketch of a parrot with labels. It contains all the details about the name, colors, size etc. Based on these descriptions, we can study about the parrot. Here, parrot is an object.
1077
+
1078
+
The example for class of parrot can be :
1079
+
```
1080
+
class Parrot:
1081
+
pass
1082
+
```
1083
+
Here, we use class keyword to define an empty class Parrot. From class, we construct instances. An instance is a specific object created from a particular class.
1084
+
1085
+
- Object
1086
+
An object (instance) is an instantiation of a class. When class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated.
1087
+
1088
+
The example for object of parrot class can be:
1089
+
```
1090
+
obj = Parrot()
1091
+
```
1092
+
Here, obj is object of class Parrot.
1093
+
1094
+
Suppose we have details of parrot. Now, we are going to show how to build the class and objects of parrot.
1095
+
```
1096
+
class Parrot:
1097
+
1098
+
# class attribute
1099
+
species = "bird"
1100
+
1101
+
# instance attribute
1102
+
def __init__(self, name, age):
1103
+
self.name = name
1104
+
self.age = age
1105
+
1106
+
# instantiate the Parrot class
1107
+
blu = Parrot("Blu", 10)
1108
+
woo = Parrot("Woo", 15)
1109
+
1110
+
# access the class attributes
1111
+
print("Blu is a {}".format(blu.__class__.species))
1112
+
print("Woo is also a {}".format(woo.__class__.species))
1113
+
1114
+
# access the instance attributes
1115
+
print("{} is {} years old".format( blu.name, blu.age))
1116
+
print("{} is {} years old".format( woo.name, woo.age))
1117
+
```
1118
+
1119
+
- Methods
1120
+
Methods are functions defined inside the body of a class. They are used to define the behaviors of an object.
1121
+
```
1122
+
class Parrot:
1123
+
1124
+
# instance attributes
1125
+
def __init__(self, name, age):
1126
+
self.name = name
1127
+
self.age = age
1128
+
1129
+
# instance method
1130
+
def sing(self, song):
1131
+
return "{} sings {}".format(self.name, song)
1132
+
1133
+
def dance(self):
1134
+
return "{} is now dancing".format(self.name)
1135
+
1136
+
# instantiate the object
1137
+
blu = Parrot("Blu", 10)
1138
+
1139
+
# call our instance methods
1140
+
print(blu.sing("'Happy'"))
1141
+
print(blu.dance())
1142
+
```
1143
+
1144
+
- Inheritance
1145
+
Inheritance is a way of creating new class for using details of existing class without modifying it. The newly formed class is a derived class (or child class). Similarly, the existing class is a base class (or parent class).
1146
+
1147
+
```
1148
+
# parent class
1149
+
class Bird:
1150
+
1151
+
def __init__(self):
1152
+
print("Bird is ready")
1153
+
1154
+
def whoisThis(self):
1155
+
print("Bird")
1156
+
1157
+
def swim(self):
1158
+
print("Swim faster")
1159
+
1160
+
# child class
1161
+
class Penguin(Bird):
1162
+
1163
+
def __init__(self):
1164
+
# call super() function
1165
+
super().__init__()
1166
+
print("Penguin is ready")
1167
+
1168
+
def whoisThis(self):
1169
+
print("Penguin")
1170
+
1171
+
def run(self):
1172
+
print("Run faster")
1173
+
1174
+
peggy = Penguin()
1175
+
peggy.whoisThis()
1176
+
peggy.swim()
1177
+
peggy.run()
1178
+
```
1179
+
1180
+
- Encapsulation
1181
+
Using OOP in Python, we can restrict access to methods and variables. This prevent data from direct modification which is called encapsulation. In Python, we denote private attribute using underscore as prefix i.e single "_ "or double "__".
Polymorphism is an ability (in OOP) to use common interface for multiple form (data types).
1209
+
1210
+
Suppose, we need to color a shape, there are multiple shape option (rectangle, square, circle). However we could use same method to color any shape. This concept is called Polymorphism.
0 commit comments