├── OOPS in Python.ipynb
└── README.md
/OOPS in Python.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "9276f0f3",
6 | "metadata": {},
7 | "source": [
8 | "# OOPS IN PYTHON"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "a80c7ff9",
14 | "metadata": {},
15 | "source": [
16 | "### Class\n",
17 | "A Class is like an object constructor, or a \"blueprint\" for creating objects."
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "id": "07ef0488",
23 | "metadata": {},
24 | "source": [
25 | "### Data Member\n",
26 | "A class variable or instance variable that holds data associated with a class and its objects."
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "id": "1fc8fc7c",
32 | "metadata": {},
33 | "source": [
34 | "### Function Overloading\n",
35 | "The assignment of more than one behavior to a particular function."
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "id": "54f4b25c",
41 | "metadata": {},
42 | "source": [
43 | "### Pass statement\n",
44 | "Class definitions cannot be empty, but if you for some reason have a class definition with no content, put in the pass statement to avoid getting an error."
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": 1,
50 | "id": "82f7ffe6",
51 | "metadata": {},
52 | "outputs": [],
53 | "source": [
54 | "#empty class\n",
55 | "class car:\n",
56 | " pass"
57 | ]
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "id": "6d6a6970",
62 | "metadata": {},
63 | "source": [
64 | "### Object\n",
65 | "Objects are instances of the class."
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "execution_count": 2,
71 | "id": "359d5fec",
72 | "metadata": {},
73 | "outputs": [
74 | {
75 | "data": {
76 | "text/plain": [
77 | "<__main__.car at 0x26e0b4870d0>"
78 | ]
79 | },
80 | "execution_count": 2,
81 | "metadata": {},
82 | "output_type": "execute_result"
83 | }
84 | ],
85 | "source": [
86 | "car1=car() #instance/object created\n",
87 | "car1"
88 | ]
89 | },
90 | {
91 | "cell_type": "markdown",
92 | "id": "d0cb46a1",
93 | "metadata": {},
94 | "source": [
95 | "### Attributes\n",
96 | "Attributes of a class are function objects that define corresponding methods of its instances. They are used to implement access controls of the classes. Attributes of a class can also be accessed using the following built-in methods and functions : getattr() – This function is used to access the attribute of object."
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 3,
102 | "id": "b428d260",
103 | "metadata": {},
104 | "outputs": [
105 | {
106 | "name": "stdout",
107 | "output_type": "stream",
108 | "text": [
109 | "4\n"
110 | ]
111 | }
112 | ],
113 | "source": [
114 | "car1.windows=5 #attributes added\n",
115 | "car1.doors=4\n",
116 | "print(car1.doors)"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": 4,
122 | "id": "30c08b7e",
123 | "metadata": {},
124 | "outputs": [],
125 | "source": [
126 | "car2=car() #instance created\n",
127 | "car2.seats=7 #attributes added\n",
128 | "car2.windows=5"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": 5,
134 | "id": "6ca5cae9",
135 | "metadata": {},
136 | "outputs": [
137 | {
138 | "name": "stdout",
139 | "output_type": "stream",
140 | "text": [
141 | "7\n"
142 | ]
143 | }
144 | ],
145 | "source": [
146 | "print(car2.seats) #no. of attributes added by this method are not fixed"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 6,
152 | "id": "df9bc090",
153 | "metadata": {},
154 | "outputs": [
155 | {
156 | "data": {
157 | "text/plain": [
158 | "['__class__',\n",
159 | " '__delattr__',\n",
160 | " '__dict__',\n",
161 | " '__dir__',\n",
162 | " '__doc__',\n",
163 | " '__eq__',\n",
164 | " '__format__',\n",
165 | " '__ge__',\n",
166 | " '__getattribute__',\n",
167 | " '__gt__',\n",
168 | " '__hash__',\n",
169 | " '__init__',\n",
170 | " '__init_subclass__',\n",
171 | " '__le__',\n",
172 | " '__lt__',\n",
173 | " '__module__',\n",
174 | " '__ne__',\n",
175 | " '__new__',\n",
176 | " '__reduce__',\n",
177 | " '__reduce_ex__',\n",
178 | " '__repr__',\n",
179 | " '__setattr__',\n",
180 | " '__sizeof__',\n",
181 | " '__str__',\n",
182 | " '__subclasshook__',\n",
183 | " '__weakref__',\n",
184 | " 'seats',\n",
185 | " 'windows']"
186 | ]
187 | },
188 | "execution_count": 6,
189 | "metadata": {},
190 | "output_type": "execute_result"
191 | }
192 | ],
193 | "source": [
194 | "dir(car2)"
195 | ]
196 | },
197 | {
198 | "cell_type": "markdown",
199 | "id": "fc10724e",
200 | "metadata": {},
201 | "source": [
202 | "### Method\n",
203 | "Objects can also contain methods. Methods in objects are functions that belong to the object."
204 | ]
205 | },
206 | {
207 | "cell_type": "markdown",
208 | "id": "4d5b2f48",
209 | "metadata": {},
210 | "source": [
211 | "### __init__ Function\n",
212 | "All classes have a function called __init__(), which is always executed when the class is being initiated. \n",
213 | "Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created.\n",
214 | "The __init__() function is called automatically every time the class is being used to create a new object.\n"
215 | ]
216 | },
217 | {
218 | "cell_type": "markdown",
219 | "id": "f6e94171",
220 | "metadata": {},
221 | "source": [
222 | "### Self parameter\n",
223 | "The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.\n",
224 | "It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class."
225 | ]
226 | },
227 | {
228 | "cell_type": "code",
229 | "execution_count": 7,
230 | "id": "07fb17d5",
231 | "metadata": {},
232 | "outputs": [],
233 | "source": [
234 | "#All the class variables are public\n",
235 | "class Bus:\n",
236 | " def __init__(self, window, door, enginetype): #constructor or in-built method of class\n",
237 | " self.windows = window #self takes reference of the object being created and adds the attributes accordingly\n",
238 | " self.doors = door\n",
239 | " self.enginetype = enginetype\n",
240 | " def self_driving(self): #another method of the class\n",
241 | " return \"This is a {} car\".format(self.enginetype)"
242 | ]
243 | },
244 | {
245 | "cell_type": "markdown",
246 | "id": "282fb435",
247 | "metadata": {},
248 | "source": [
249 | "### Constructor\n",
250 | "Constructors are generally used for instantiating an object. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. In Python the __init__() method is called the constructor and is always called when an object is created."
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "execution_count": 8,
256 | "id": "bbbe676a",
257 | "metadata": {},
258 | "outputs": [],
259 | "source": [
260 | "Bus1=Bus(20,2,'diesel')"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": 9,
266 | "id": "d11e017f",
267 | "metadata": {},
268 | "outputs": [
269 | {
270 | "name": "stdout",
271 | "output_type": "stream",
272 | "text": [
273 | "diesel\n"
274 | ]
275 | }
276 | ],
277 | "source": [
278 | "print(Bus1.enginetype)"
279 | ]
280 | },
281 | {
282 | "cell_type": "code",
283 | "execution_count": 10,
284 | "id": "25077490",
285 | "metadata": {},
286 | "outputs": [
287 | {
288 | "data": {
289 | "text/plain": [
290 | "'This is a diesel car'"
291 | ]
292 | },
293 | "execution_count": 10,
294 | "metadata": {},
295 | "output_type": "execute_result"
296 | }
297 | ],
298 | "source": [
299 | "Bus1.self_driving() #method created inside a class"
300 | ]
301 | },
302 | {
303 | "cell_type": "markdown",
304 | "id": "ea01b3b2",
305 | "metadata": {},
306 | "source": [
307 | "### Inheritance\n",
308 | "Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class."
309 | ]
310 | },
311 | {
312 | "cell_type": "code",
313 | "execution_count": 11,
314 | "id": "588aea1f",
315 | "metadata": {},
316 | "outputs": [],
317 | "source": [
318 | "class volvo(Bus):\n",
319 | " def __init__(self, window, door, enginetype, ai):\n",
320 | " super().__init__(window, door, enginetype) #access from parent class\n",
321 | " self.ai = ai\n",
322 | " def selfdriving(self):\n",
323 | " return \"Volvo supports self-driving.\""
324 | ]
325 | },
326 | {
327 | "cell_type": "code",
328 | "execution_count": 12,
329 | "id": "d5c6105e",
330 | "metadata": {},
331 | "outputs": [],
332 | "source": [
333 | "Shyamali = volvo(20,2,'diesel',True)"
334 | ]
335 | },
336 | {
337 | "cell_type": "code",
338 | "execution_count": 13,
339 | "id": "92e8160a",
340 | "metadata": {},
341 | "outputs": [
342 | {
343 | "data": {
344 | "text/plain": [
345 | "['__class__',\n",
346 | " '__delattr__',\n",
347 | " '__dict__',\n",
348 | " '__dir__',\n",
349 | " '__doc__',\n",
350 | " '__eq__',\n",
351 | " '__format__',\n",
352 | " '__ge__',\n",
353 | " '__getattribute__',\n",
354 | " '__gt__',\n",
355 | " '__hash__',\n",
356 | " '__init__',\n",
357 | " '__init_subclass__',\n",
358 | " '__le__',\n",
359 | " '__lt__',\n",
360 | " '__module__',\n",
361 | " '__ne__',\n",
362 | " '__new__',\n",
363 | " '__reduce__',\n",
364 | " '__reduce_ex__',\n",
365 | " '__repr__',\n",
366 | " '__setattr__',\n",
367 | " '__sizeof__',\n",
368 | " '__str__',\n",
369 | " '__subclasshook__',\n",
370 | " '__weakref__',\n",
371 | " 'ai',\n",
372 | " 'doors',\n",
373 | " 'enginetype',\n",
374 | " 'self_driving',\n",
375 | " 'selfdriving',\n",
376 | " 'windows']"
377 | ]
378 | },
379 | "execution_count": 13,
380 | "metadata": {},
381 | "output_type": "execute_result"
382 | }
383 | ],
384 | "source": [
385 | "dir(Shyamali)"
386 | ]
387 | },
388 | {
389 | "cell_type": "code",
390 | "execution_count": 14,
391 | "id": "da39d511",
392 | "metadata": {},
393 | "outputs": [
394 | {
395 | "data": {
396 | "text/plain": [
397 | "20"
398 | ]
399 | },
400 | "execution_count": 14,
401 | "metadata": {},
402 | "output_type": "execute_result"
403 | }
404 | ],
405 | "source": [
406 | "Shyamali.windows"
407 | ]
408 | },
409 | {
410 | "cell_type": "code",
411 | "execution_count": 15,
412 | "id": "1279be75",
413 | "metadata": {},
414 | "outputs": [
415 | {
416 | "data": {
417 | "text/plain": [
418 | "True"
419 | ]
420 | },
421 | "execution_count": 15,
422 | "metadata": {},
423 | "output_type": "execute_result"
424 | }
425 | ],
426 | "source": [
427 | "Shyamali.ai"
428 | ]
429 | },
430 | {
431 | "cell_type": "code",
432 | "execution_count": 16,
433 | "id": "c9e8422e",
434 | "metadata": {},
435 | "outputs": [
436 | {
437 | "data": {
438 | "text/plain": [
439 | "bool"
440 | ]
441 | },
442 | "execution_count": 16,
443 | "metadata": {},
444 | "output_type": "execute_result"
445 | }
446 | ],
447 | "source": [
448 | "type(Shyamali.ai)"
449 | ]
450 | },
451 | {
452 | "cell_type": "code",
453 | "execution_count": 17,
454 | "id": "d51224a5",
455 | "metadata": {},
456 | "outputs": [
457 | {
458 | "data": {
459 | "text/plain": [
460 | "'Volvo supports self-driving.'"
461 | ]
462 | },
463 | "execution_count": 17,
464 | "metadata": {},
465 | "output_type": "execute_result"
466 | }
467 | ],
468 | "source": [
469 | "Shyamali.selfdriving()"
470 | ]
471 | },
472 | {
473 | "cell_type": "markdown",
474 | "id": "9c4c1893",
475 | "metadata": {},
476 | "source": [
477 | "### Public, Protected and Private variables\n",
478 | "There are three types of access modifiers in Python: public, private, and protected. Variables with the public access modifiers can be accessed anywhere inside or outside the class, the private variables can only be accessed inside the class, while protected variables can be accessed within the same package."
479 | ]
480 | },
481 | {
482 | "cell_type": "markdown",
483 | "id": "b42d980f",
484 | "metadata": {},
485 | "source": [
486 | "#### PROTECTED"
487 | ]
488 | },
489 | {
490 | "cell_type": "markdown",
491 | "id": "9decdc1b",
492 | "metadata": {},
493 | "source": [
494 | "The members of a class that are declared protected are only accessible to a class derived from it. Data members of a class are declared protected by adding a single underscore '_' symbol before the data member of that class."
495 | ]
496 | },
497 | {
498 | "cell_type": "code",
499 | "execution_count": 18,
500 | "id": "e7b81c81",
501 | "metadata": {},
502 | "outputs": [],
503 | "source": [
504 | "### All the class variables are protected\n",
505 | "class Car():\n",
506 | " def __init__(self,windows,doors,enginetype):\n",
507 | " self._windows=windows\n",
508 | " self._doors=doors\n",
509 | " self._enginetype=enginetype"
510 | ]
511 | },
512 | {
513 | "cell_type": "code",
514 | "execution_count": 19,
515 | "id": "2deadce7",
516 | "metadata": {},
517 | "outputs": [],
518 | "source": [
519 | "class Truck(Car):\n",
520 | " def __init__(self,windows,doors,enginetype,horsepower):\n",
521 | " super().__init__(windows,doors,enginetype)\n",
522 | " self.horsepowwer=horsepower"
523 | ]
524 | },
525 | {
526 | "cell_type": "code",
527 | "execution_count": 20,
528 | "id": "8fd8ffe4",
529 | "metadata": {},
530 | "outputs": [
531 | {
532 | "data": {
533 | "text/plain": [
534 | "['__class__',\n",
535 | " '__delattr__',\n",
536 | " '__dict__',\n",
537 | " '__dir__',\n",
538 | " '__doc__',\n",
539 | " '__eq__',\n",
540 | " '__format__',\n",
541 | " '__ge__',\n",
542 | " '__getattribute__',\n",
543 | " '__gt__',\n",
544 | " '__hash__',\n",
545 | " '__init__',\n",
546 | " '__init_subclass__',\n",
547 | " '__le__',\n",
548 | " '__lt__',\n",
549 | " '__module__',\n",
550 | " '__ne__',\n",
551 | " '__new__',\n",
552 | " '__reduce__',\n",
553 | " '__reduce_ex__',\n",
554 | " '__repr__',\n",
555 | " '__setattr__',\n",
556 | " '__sizeof__',\n",
557 | " '__str__',\n",
558 | " '__subclasshook__',\n",
559 | " '__weakref__',\n",
560 | " '_doors',\n",
561 | " '_enginetype',\n",
562 | " '_windows',\n",
563 | " 'horsepowwer']"
564 | ]
565 | },
566 | "execution_count": 20,
567 | "metadata": {},
568 | "output_type": "execute_result"
569 | }
570 | ],
571 | "source": [
572 | "truck=Truck(4,4,\"Diesel\",4000)\n",
573 | "dir(truck)"
574 | ]
575 | },
576 | {
577 | "cell_type": "code",
578 | "execution_count": 21,
579 | "id": "94125dcf",
580 | "metadata": {},
581 | "outputs": [],
582 | "source": [
583 | "truck._doors=5"
584 | ]
585 | },
586 | {
587 | "cell_type": "code",
588 | "execution_count": 22,
589 | "id": "193ac98e",
590 | "metadata": {},
591 | "outputs": [
592 | {
593 | "data": {
594 | "text/plain": [
595 | "5"
596 | ]
597 | },
598 | "execution_count": 22,
599 | "metadata": {},
600 | "output_type": "execute_result"
601 | }
602 | ],
603 | "source": [
604 | "truck._doors"
605 | ]
606 | },
607 | {
608 | "cell_type": "markdown",
609 | "id": "51d29eb2",
610 | "metadata": {},
611 | "source": [
612 | "#### PRIVATE "
613 | ]
614 | },
615 | {
616 | "cell_type": "markdown",
617 | "id": "8b005789",
618 | "metadata": {},
619 | "source": [
620 | "In the context of class, private means the attributes are only available for the members of the class not for the outside of the class."
621 | ]
622 | },
623 | {
624 | "cell_type": "code",
625 | "execution_count": 23,
626 | "id": "01124dbe",
627 | "metadata": {},
628 | "outputs": [],
629 | "source": [
630 | "### private\n",
631 | "class Car():\n",
632 | " def __init__(self,windows,doors,enginetype):\n",
633 | " self.__windows=windows\n",
634 | " self.__doors=doors\n",
635 | " self.__enginetype=enginetype"
636 | ]
637 | },
638 | {
639 | "cell_type": "code",
640 | "execution_count": 24,
641 | "id": "ec298653",
642 | "metadata": {},
643 | "outputs": [],
644 | "source": [
645 | "audi=Car(4,4,\"Diesel\")"
646 | ]
647 | },
648 | {
649 | "cell_type": "code",
650 | "execution_count": 25,
651 | "id": "40c56106",
652 | "metadata": {},
653 | "outputs": [],
654 | "source": [
655 | "audi._Car__doors=5"
656 | ]
657 | },
658 | {
659 | "cell_type": "code",
660 | "execution_count": 26,
661 | "id": "3b8c0da6",
662 | "metadata": {},
663 | "outputs": [
664 | {
665 | "data": {
666 | "text/plain": [
667 | "['_Car__doors',\n",
668 | " '_Car__enginetype',\n",
669 | " '_Car__windows',\n",
670 | " '__class__',\n",
671 | " '__delattr__',\n",
672 | " '__dict__',\n",
673 | " '__dir__',\n",
674 | " '__doc__',\n",
675 | " '__eq__',\n",
676 | " '__format__',\n",
677 | " '__ge__',\n",
678 | " '__getattribute__',\n",
679 | " '__gt__',\n",
680 | " '__hash__',\n",
681 | " '__init__',\n",
682 | " '__init_subclass__',\n",
683 | " '__le__',\n",
684 | " '__lt__',\n",
685 | " '__module__',\n",
686 | " '__ne__',\n",
687 | " '__new__',\n",
688 | " '__reduce__',\n",
689 | " '__reduce_ex__',\n",
690 | " '__repr__',\n",
691 | " '__setattr__',\n",
692 | " '__sizeof__',\n",
693 | " '__str__',\n",
694 | " '__subclasshook__',\n",
695 | " '__weakref__']"
696 | ]
697 | },
698 | "execution_count": 26,
699 | "metadata": {},
700 | "output_type": "execute_result"
701 | }
702 | ],
703 | "source": [
704 | "dir(audi)"
705 | ]
706 | },
707 | {
708 | "cell_type": "markdown",
709 | "id": "fe00086e",
710 | "metadata": {},
711 | "source": [
712 | "### Name Mangling"
713 | ]
714 | },
715 | {
716 | "cell_type": "markdown",
717 | "id": "3f86767c",
718 | "metadata": {},
719 | "source": [
720 | "Python performs name mangling on private attributes. Every member with a double underscore will be changed to _object._class__variable."
721 | ]
722 | },
723 | {
724 | "cell_type": "code",
725 | "execution_count": 27,
726 | "id": "f3efc1bc",
727 | "metadata": {},
728 | "outputs": [
729 | {
730 | "data": {
731 | "text/plain": [
732 | "['_Car__doors',\n",
733 | " '_Car__enginetype',\n",
734 | " '_Car__windows',\n",
735 | " '__class__',\n",
736 | " '__delattr__',\n",
737 | " '__dict__',\n",
738 | " '__dir__',\n",
739 | " '__doc__',\n",
740 | " '__eq__',\n",
741 | " '__format__',\n",
742 | " '__ge__',\n",
743 | " '__getattribute__',\n",
744 | " '__gt__',\n",
745 | " '__hash__',\n",
746 | " '__init__',\n",
747 | " '__init_subclass__',\n",
748 | " '__le__',\n",
749 | " '__lt__',\n",
750 | " '__module__',\n",
751 | " '__ne__',\n",
752 | " '__new__',\n",
753 | " '__reduce__',\n",
754 | " '__reduce_ex__',\n",
755 | " '__repr__',\n",
756 | " '__setattr__',\n",
757 | " '__sizeof__',\n",
758 | " '__str__',\n",
759 | " '__subclasshook__',\n",
760 | " '__weakref__']"
761 | ]
762 | },
763 | "execution_count": 27,
764 | "metadata": {},
765 | "output_type": "execute_result"
766 | }
767 | ],
768 | "source": [
769 | "dir(audi)"
770 | ]
771 | },
772 | {
773 | "cell_type": "markdown",
774 | "id": "be08bdde",
775 | "metadata": {},
776 | "source": [
777 | "Thus, Python does not provide the functionality of a private access-modifier, as compared to classical programming languages.\n",
778 | "The process entirely relies on the programmer."
779 | ]
780 | },
781 | {
782 | "cell_type": "markdown",
783 | "id": "a804be1a",
784 | "metadata": {},
785 | "source": [
786 | "So a responsible programmer, upon seeing an attribute with such a naming convention, would refrain from accessing it outside its scope. This also wouldn’t be good to use in cases where fellow programmers aren’t aware of such naming conventions."
787 | ]
788 | }
789 | ],
790 | "metadata": {
791 | "kernelspec": {
792 | "display_name": "Python 3 (ipykernel)",
793 | "language": "python",
794 | "name": "python3"
795 | },
796 | "language_info": {
797 | "codemirror_mode": {
798 | "name": "ipython",
799 | "version": 3
800 | },
801 | "file_extension": ".py",
802 | "mimetype": "text/x-python",
803 | "name": "python",
804 | "nbconvert_exporter": "python",
805 | "pygments_lexer": "ipython3",
806 | "version": "3.9.7"
807 | }
808 | },
809 | "nbformat": 4,
810 | "nbformat_minor": 5
811 | }
812 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # OOP-Python-
2 |
3 |
4 | Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic.
5 | Though the underlying concept is almost the same, its implementation varies for every programming language.
6 | This repository contains the basic concepts that one should know to implement OOP in Python.
7 |
8 |
9 |
10 |
11 | ## Contribute
12 |
13 | This is a contribution friendly repository. You can add and improve any code snippet or concept, your contribution is welcomed ❤️!
14 |
15 |
16 | ## Connect with me: [](https://www.linkedin.com/in/poulami-paul-69a988220/) [](https://twitter.com/_Lustre_1_)
17 |
18 | ## Give it a 🌟 if you ❤ this notebook.
19 |
20 | ## Happy Coding 👨💻
21 |
--------------------------------------------------------------------------------