|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 | | - "# OOPS:\n", |
| 7 | + "# Object Oriented Programming - Part 1\n", |
| 8 | + "### OOP:\n", |
8 | 9 | "\n",
|
9 | 10 | "Object Oriented Programming or OOP is working with classes and objects. It involves interaction between objects having various attributes.\n",
|
10 | 11 | "\n",
|
|
26 | 27 | "source": [
|
27 | 28 | "## Class:\n",
|
28 | 29 | "\n",
|
29 | | - "It is a structure or a building block (a code block) representing or defining the attributes (features and behaviour) of similar parameters.\n", |
| 30 | + "It is a structure, a blueprint or a building block (a code block) representing or defining the attributes (features and behaviour) of similar parameters.\n", |
30 | 31 | "\n",
|
31 | 32 | "### Definition:\n",
|
32 | 33 | "Using `class` keyword.\n",
|
|
112 | 113 | "cell_type": "markdown",
|
113 | 114 | "metadata": {},
|
114 | 115 | "source": [
|
115 | | - "## Defining attributes in a class\n", |
| 116 | + "## Defining attributes and behavior in a class\n", |
116 | 117 | "\n",
|
117 | | - "Attributes or Properties are generally of two types:\n", |
118 | | - "1. Fields (variables)\n", |
119 | | - "2. Methods (functions)\n", |
| 118 | + "An object is generally defined by the follwing:\n", |
| 119 | + "1. Fields (variables) or Attributes\n", |
| 120 | + "2. Methods (functions) or Behavior\n", |
120 | 121 | "\n",
|
121 | 122 | "### Fields\n",
|
122 | 123 | "The `fields` are the variables or containers that store data used and related to an object/Class\n",
|
123 | 124 | "\n",
|
124 | 125 | "Types: \n",
|
125 | 126 | "1. Instance fields\n",
|
126 | | - "2. Class or static fields\n" |
| 127 | + "2. Class or static fields\n", |
| 128 | + "\n", |
| 129 | + "#### Instance fields:\n", |
| 130 | + "The instance fields are variables that are associated with an object, an instance of the class.\n", |
| 131 | + "\n", |
| 132 | + "The data they have may vary from object to object and are independent. The field of one instance cannot be accessed by the other. \n", |
| 133 | + "The instance variables are accessed as follows:\n", |
| 134 | + "```python\n", |
| 135 | + "# Inside the class using `self` keyword\n", |
| 136 | + "self.instance_variable\n", |
| 137 | + "\n", |
| 138 | + "# Outside the class using the object name\n", |
| 139 | + "object_name.instance_variable\n", |
| 140 | + "```\n", |
| 141 | + "\n", |
| 142 | + "#### Class fields:\n", |
| 143 | + "The class variables are variables that are common throughout the instances of the defined class and can be accessed by all of them.\n", |
| 144 | + "\n", |
| 145 | + "Modifying data in one object changes it for all the present instances. \n", |
| 146 | + "They are accessed inside and outside the class definition as follows:\n", |
| 147 | + "```python\n", |
| 148 | + "# Using class name to access static variables\n", |
| 149 | + "Class_name.class_variable\n", |
| 150 | + "```" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": 1, |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [ |
| 158 | + { |
| 159 | + "name": "stdout", |
| 160 | + "output_type": "stream", |
| 161 | + "text": [ |
| 162 | + "Prabhu\n", |
| 163 | + "Mano\n", |
| 164 | + "2\n" |
| 165 | + ] |
| 166 | + } |
| 167 | + ], |
| 168 | + "source": [ |
| 169 | + "class Student_details:\n", |
| 170 | + " student_count = 0\n", |
| 171 | + " def __init__(self, name):\n", |
| 172 | + " self.name = name # Instance variable\n", |
| 173 | + " Student_details.student_count += 1 # Class Variable\n", |
| 174 | + "\n", |
| 175 | + "s = Student_details(\"Prabhu\")\n", |
| 176 | + "print(s.name) # Instance variable\n", |
| 177 | + "s1 = Student_details(\"Mano\")\n", |
| 178 | + "print(s1.name) # Instance variable\n", |
| 179 | + "print(Student_details.student_count) # Class variable" |
127 | 180 | ]
|
128 | 181 | },
|
129 | 182 | {
|
130 | 183 | "cell_type": "markdown",
|
131 | 184 | "metadata": {},
|
132 | 185 | "source": [
|
| 186 | + "### Methods\n", |
133 | 187 | "The `methods` are block of code that, like functions, execute a specific task\n",
|
134 | 188 | "\n",
|
135 | 189 | "Though a method and function are defined in the same way, they have differences.\n",
|
|
145 | 199 | "\n",
|
146 | 200 | "(remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).\n",
|
147 | 201 | "\n",
|
| 202 | + "Source: [What's the difference between a method and a function? - StackOverflow](https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function#155655)\n", |
| 203 | + "\n", |
148 | 204 | "Types:\n",
|
149 | 205 | "1. Instance methods\n",
|
150 | | - "2. Static methods" |
| 206 | + "2. Static methods\n", |
| 207 | + "\n", |
| 208 | + "#### Instance methods:\n", |
| 209 | + "Instance methods are methods/behavior that are associated with objects. When an object calls one of its instnace methods, the instance method gets implicitly passed object and uses the data it gets with other required parameters to do the task. \n", |
| 210 | + "Definition and access:\n", |
| 211 | + "```python\n", |
| 212 | + "# Definition\n", |
| 213 | + "def instance_method(self[, ...]): # the self keyword indicates the implicit passing of the object\n", |
| 214 | + " # Statements\n", |
| 215 | + "\n", |
| 216 | + "# access inside the class (in another instance method)\n", |
| 217 | + "self.instance_method(...)\n", |
| 218 | + "\n", |
| 219 | + "# Access outside the class\n", |
| 220 | + "obj.instance_method(...)\n", |
| 221 | + "```\n", |
| 222 | + "***Note:*** It is to be noted that an instance method can be called only inside another instance method\n", |
| 223 | + "\n", |
| 224 | + "#### Static or Class method:\n", |
| 225 | + "A static method is a method that is common for all objects. It is equivalent of function being defined outside the class.\n", |
| 226 | + "\n", |
| 227 | + "A static method is identified by the decorator `@staticmethod` and has implicit object passing using `self` keyword.\n", |
| 228 | + "\n", |
| 229 | + "Definition and Access:\n", |
| 230 | + "```python\n", |
| 231 | + "@staticmethod\n", |
| 232 | + "def class_method([...]):\n", |
| 233 | + " #Statement(s)\n", |
| 234 | + "\n", |
| 235 | + "# accessing the method using class name\n", |
| 236 | + "Class_name.class_method([...])\n", |
| 237 | + "```\n", |
| 238 | + "The static method can be called inside another static method or instance method(s)" |
151 | 239 | ]
|
152 | 240 | },
|
153 | 241 | {
|
|
165 | 253 | "```\n",
|
166 | 254 | "\n",
|
167 | 255 | "### Initializer:\n",
|
168 | | - "A method that initializes the object (instance) created by call with the parameters passed. \n", |
| 256 | + "An instance method that initializes the object (instance) created by call with the parameters passed. \n", |
169 | 257 | "\n",
|
170 | 258 | "The `__new__()` method calls this automatically.\n",
|
171 | 259 | "\n",
|
|
216 | 304 | },
|
217 | 305 | {
|
218 | 306 | "cell_type": "code",
|
219 | | - "execution_count": 9, |
| 307 | + "execution_count": 2, |
220 | 308 | "metadata": {},
|
221 | 309 | "outputs": [],
|
222 | 310 | "source": [
|
223 | 311 | "class Student_details:\n",
|
224 | 312 | " def __init__(self, rn = 0, st_name = 'Name'): # Parametric Constructor\n",
|
225 | 313 | " self.roll_no = rn\n",
|
226 | 314 | " self.name = st_name\n",
|
227 | | - "# print('__init__ file')\n", |
| 315 | + " # print('__init__ file')\n", |
228 | 316 | " \n",
|
229 | 317 | " def study(self):\n",
|
230 | | - " print('Studying....')" |
| 318 | + " print('Studying....')\n", |
| 319 | + " @staticmethod\n", |
| 320 | + " def work():\n", |
| 321 | + " print(\"Working...\")" |
231 | 322 | ]
|
232 | 323 | },
|
233 | 324 | {
|
234 | 325 | "cell_type": "code",
|
235 | | - "execution_count": 10, |
| 326 | + "execution_count": 3, |
236 | 327 | "metadata": {},
|
237 | 328 | "outputs": [
|
238 | 329 | {
|
239 | 330 | "name": "stdout",
|
240 | 331 | "output_type": "stream",
|
241 | 332 | "text": [
|
242 | | - "Roll No: 2, Name: Rahul\n" |
| 333 | + "Roll No: 2, Name: Rahul\n", |
| 334 | + "Working...\n" |
243 | 335 | ]
|
244 | 336 | }
|
245 | 337 | ],
|
246 | 338 | "source": [
|
247 | 339 | "st2 = Student_details(2, 'Rahul')\n",
|
248 | | - "print(f'Roll No: {st2.roll_no}, Name: {st2.name}')" |
| 340 | + "print(f'Roll No: {st2.roll_no}, Name: {st2.name}')\n", |
| 341 | + "Student_details.work()" |
249 | 342 | ]
|
250 | 343 | },
|
251 | 344 | {
|
|
295 | 388 | "It is advised for best programming practice to avoid calling private attributes outside the class. \n",
|
296 | 389 | "But if needed, use the following syntax:\n",
|
297 | 390 | "\n",
|
298 | | - "`obj._classname__attribute`" |
299 | | - ] |
300 | | - }, |
301 | | - { |
302 | | - "cell_type": "markdown", |
303 | | - "metadata": {}, |
304 | | - "source": [ |
305 | | - "## Calling a method in another method inside the class:\n", |
306 | | - "\n", |
307 | | - "Use self keyword to call the function." |
308 | | - ] |
309 | | - }, |
310 | | - { |
311 | | - "cell_type": "markdown", |
312 | | - "metadata": {}, |
313 | | - "source": [ |
314 | | - "## Definining run time class attributes:" |
315 | | - ] |
316 | | - }, |
317 | | - { |
318 | | - "cell_type": "markdown", |
319 | | - "metadata": {}, |
320 | | - "source": [ |
321 | | - "## Methods of attributes:" |
| 391 | + "```python\n", |
| 392 | + "obj._classname__attribute\n", |
| 393 | + "```" |
322 | 394 | ]
|
323 | 395 | },
|
324 | | - { |
325 | | - "cell_type": "code", |
326 | | - "execution_count": null, |
327 | | - "metadata": {}, |
328 | | - "outputs": [], |
329 | | - "source": [] |
330 | | - }, |
331 | 396 | {
|
332 | 397 | "cell_type": "markdown",
|
333 | 398 | "metadata": {},
|
|
0 commit comments