Loading ...
SeekhoCoding
Python Syllabus Game Python Projects

Chapter 11 : OOPs In Python

आपको बता दे कि आप Python Language के उपयोग से Real Life से संबंधित Programs Create कर सकते है | आप Bank ATM , Employee Salary जैसे Real Life Projects create कर सके इस Purpose से हमने इस Chapter को add किया गया है | इस Chapter मे आप Introduction To OOPs , Classes , Objects , Class Object Methods जैसे और भी महत्वपूर्ण Topics के बारे मे जानेगे |

OOPs

OOPs का पूरा नाम Object-Oriented Programming System है | OPPs का मतलब होता है कि programming को इस तरह से लिखा जाए की लगे की चीजे real world मे desgine की जा रही है | code को clean , reusable , बडी- बडी Applications बनाने के लिए OPPs का उपयोग किया जाता है | OPPs object based है यानि की लिखा गया कोई भी code तब किसी काम का नही , जब तक उस code का object न बनाया जाए |

Class and Object

Class = Blueprint (map)
Object = Actual building (इमारत)
class एक Blueprint होती है और object Actual building होती है |

class के अन्दर variables , functions and method create किऐ जाते है | किसी class को बनाने के बाद वो class तब तक किसी काम की नही होती है | जब तक उस class का object न बनाया जाये , इसलिए class एक Blueprint और object एक building होती है | नीचे class और object को समझाने के लिए एक code दिया गया है |

1class example:
2    def example1(self):
3        print("hello,world")
4obj=example()
5obj.example1()
  • Output
  •     hello,world
      

    Code Explanation

    class example = > example नाम की एक class create की गई है |
    def example1(self) = > example1 नाम का एक function create किया गया है |
    print("hello,world") = > ये print() function Screen पर "hello,world" show करता है |
    obj = example() = > example class को run करने के लिए example class का obj नाम का object create किया गया है |
    obj.example1 = > obj object से example class के example1 नाम के function को run किया गया है |


    Purpose Of OOPs

    1. Code Design : OOPs का सबसे अधिक उपयोग Python Programming मे code Design के लिए किया जाता है | OOPs मे object and classes के उपयोग से चीजो को real-world के जैसे Design किया जा सकता है |
    2. Code Management : बडे - बडे projects मे OOPs के उपयोग से code clean , readable बनाया जा सकता है , यानि बडे - बडे code को readable बनाने के लिए OOPs का उपयोग किया जाता है |
    3. Encapsulation : Encapsulation Concept के उपयोग से projects को hide किया जा सकता है | अपने किसी Important projects को secure बनाने के लिए OOPs का उपयोग करना |
    4. complex code : OPPs object based होता है तो OPPs मे हर code का object बनाया जाता है , इसलिए OOPs के उपयोग से code को short किया जा सकता है |
    5. Inheritance : एक child class अपने parent class के code को use कर सकती है , जिसके कारण एक ही code को बार - बार लिखने की जरूरत नही होती है |


    Advantages OF OOPs

    1. Inheritance : Python Programming मे Inheritance Concept से एक child class अपने parent class के code को use कर सकती है , जिसके कारण एक ही code को बार - बार लिखने की जरूरत नही होती है | तो OOPs के use से Time की बचत होती है |
    2. Encapsulation : OOPs के Encapsulation Concept के उपयोग से किसी Data , Methods , Variables etc. को private बनाया जा सकता है , जिसके कारण कोई बाहरी person data को access नही कर सकता है | तो OOPs Data को secure बनाता है |
    3. Polymorphism : Polymorphism Concept के कारण एक ही function , method का एक से अधिक classes मे use किया जा सकता है | Example: method overriding
    4. Abstraction : Abstraction के उपयोग से main logics को hide किया जा सकता है , जिसके कारण कोई बाहरी person logics को access नही कर सकता , पर logics के काम का लाभ ले सकता है |
    5. Extensibility : कोई new Feature add करने के लिए पूरे code को बदलने की जरूरत नही होती code मे changes करके new Feature को add कर सकते है |


    Class Methods क्या होते हैं ?

    Python Language मे Class Methods Class का एक महत्वपूर्ण Concept है Class Methods का Objects से कोई संबंध नही होता है | Create की गई class को Delete करने के बजाए Class Method से Class मे changes किए जा सकते है | Class Method के उपयोग से Create की गई Class के variable को access किया जा सकता है class को modify किया जा सकता है | Python मे Class Method का उपयोग करने के लिए @classmethod decorator का उपयोग किया जाता है | नीचे Class Method से संबंधित एक Python Code दिया गया है |

    1class Student:
    2    school_name = "ABC School"
    3    @classmethod
    4    def change_school(cls, name):
    5        cls.school_name = name
    6Student.change_school("XYZ School")
    7print(Student.school_name)
        
  • Output
  • XYZ School

    Code Explanation

    class Student: = > Student नाम की एक Class create की गई है |
    school_name = "ABC School" = > school_name एक variable है , जिसमे एक School का नाम store है |
    @classmethod = > classmethod का उपयोग किया गया है |
    def change_school(cls, name): = > change_school नाम का एक Function बनाया गया है , जिसमे name नाम का variable भी create किया गया है |
    cls.school_name = name = > school_name variable की value name variable के बराबर set की गई है |
    Student.change_school("XYZ School") = > Student class के change_school function को call की गई है और name variable मे नए School का name set किया गया है |
    print(Student.school_name) = > ये print() function Student class के school_name variable को display करता है |


    Advantages Of Class Method

    1. Modify = > Class मे new features add करने का Process easy होता है |
    2. Creation = > Class Method का उपयोग करने के लिए किसी Module File या library का उपयोग करने की जरूरत नही इसके लिए केवल @classmethod का उपयोग किया जाता है |
    3. Calling = > Class Method के उपयोग से create की गई class को अलग से class का Object बनाए ही class अपने आप run हो जाती है अलग से run करने की जरूरत नही होती है |
    4. Program Lenght = > क्योंकि Create की गई Class मे समय - समय पर Changes करने पडते है जिससे Program की Lenght बडती है , लेकिन Class Method के उपयोग से class के Code बढ़ाएं बिना Class मे changes किए जा सकते है जिससे Program की Lenght कम होती है Computer Memory कम उपयोग होती है |


    Instance Methods

    Instance Method Class Method के opposite काम करता है Instance Method को Object Method के नाम से भी जाना जाता है | Instance Method Python के OOPs Concept का बहुत महत्वपूर्ण Concept है क्योंकि Beginer Python Programer लगभग Class को run करने के लिए Instance Method का उपयोग का करता है | Instance Method के उपयोग से Object के Data को Access , Data मे Modifications की जा सकती है | Instance Method के उपयोग के लिए Self Keyword का उपयोग किया जाता है | नीचे Instance Method से संबंधित एक Code दिया गया है |

    1class Student:
    2    def __init__(self, name, marks):
    3        self.name = name
    4        self.marks = marks
    5    def display(self):
    6        print("Name:", self.name)
    7        print("Marks:", self.marks)
    8s1 = Student("Rahul", 85)
    9s1.display()
        
  • Output
  • Name: Rahul
    Marks: 85

    Code Explanation

    Class Student: = > Student नाम की एक class create की गई है |
    def __init__(self, name, marks) = > एक constuctor function create किया गया है जिसमे name और marks नाम के दो variables भी create किए गए है|
    self.name = name = > name variable की value function के name variable के Equal set की गई है |
    self.marks = marks = > marks variable की value function के marks variable के बराबर set की गई है |
    def display(self) = > display नाम का एक और Function Create किया गया है |
    print(self.name) = > ये print() function name variable को display करता है |
    print(self.marks) = > ये print() function marks variable को display करता है |
    s1 = Student("Rahul", 85) = > Student class का s1 नाम का object create किया गया है |
    s1.display() = > s1 object से Student Class के display function run किया गया है |


    Fetures Of Instance Method

    1. Self = > Instance Method का उपयोग करने के लिए Self Keyword का उपयोग किया जाता है self एक built in Keyword है |
    2. Calling = > Create की गई class को Instance Method से Call करने के लिए class का हमेशा Object create किया जाता है |
    3. Access = > Instance Method से Variables को object से ही access किया जाता है |
    4. favirote = > ये Method users के favirote Methods मे से एक है |


    Diffrence Bitween Instance and Class Method

    Base Class Method Instance Method
    Access class method से Class के data को access किया जाता है | Object method से Object के data को access किया जाता है |
    Parameter Class Method के लिए First Method cls का उपयोग होता है | Instance Method के लिए First Method self का उपयोग होता है |
    decorator Class Method के लिए @classmethod का उपयोग किया जाता है | Instance Method के लिए कोई decorator नही है |
    Calling Class Object दोनों से call किया जा सकता है | Object से Call किया जाता है |
    Use Class method का उपयोग कम किया जाता है | Object Method का अधिक उपयोग किया जाता है |

    Class Variables क्या होते हैं ?

    Python Programs को create करते समय Data Store करने के लिए Variables का बहुत अधिक उपयोग किया जाता है | जब हम class create करते है तो data store करने के लिए Class के अंदर भी Variables create किए जाते है class के अंदर create किए गए इन variables को ही class variables कहा जाता है | हम Class Variables का अधिक उपयोग तब करते है जब हमे same value का एक से अधिक objects मे उपयोग करना होता है | नीचे Python code दिया गया है जिसमे एक class मे variables create किए गए है |

    1class Student:
    2    school_name = "ABC School"
    3s1 = Student()
    4print(s1.school_name)
        
  • Output
  • ABC School
    ABC School

    Code Explanation

    class Student: = > Student नाम की एक class Create की गई है |
    school_name = "ABC School" = > school_name एक variable है , जिसमे एक School का Name store है |
    s1 = Student() = > Student Class का s1 नाम का object create किया गया है |
    print(s1.school_name) = > ये print() function screen पर s1 object से school_name variable को display करता है |


    Fetures Of Class Variables

    1. Class के अंदर लेकिन method के बाहर define किया जाता है |
    2. सभी objects इसे share करते हैं |
    3. Memory बचती है क्योंकि हर object के लिए नई copy नहीं बनती |
    4. इसे class name से access करना सबसे सही तरीका होता है |


    Instance Variables

    Instance Variables का Concept Class Variables के जैसा ही है , लेकिन Class variables के opposite है Instance variables से मतलब है ये ऐसे Variables होते है जिनको Object के according Create किया जाता है | इन Variables की Counting Objects पर Based है जितने किसी class के Object create किए जाते है उतने ही Instance Variables create किए जाते है | ये Variables .self से connected होते है | नीचे Instance Variables से संबंधित Python code दिया गया है |

    1class Student:
    2    def __init__(self, name, marks):
    3        self.name = name
    4        self.marks = marks
    5s1 = Student("Rahul", 85)
    6s2 = Student("Amit", 90)
    7print(s1.name, s1.marks)
    8print(s2.name, s2.marks)
        
  • Output
  • ABC School
    ABC School

    Code Explanation

    class Student: = > Student नाम की एक Class Create की गए है |
    def __init__(self, name, marks): = > एक constuctor Function create किया गया है , जिसमे दो variables दिए गए है |
    self.name = name = > name variable की value function के name variable के बराबर set की गई है |
    self.marks = marks = > marks variable की value function के marks variable के बराबर set की गई है |
    s1 = Student("Rahul", 85) = > Student Class का s1 नाम का object create किया गया है name और marks variable को values दि गई है |
    print(s1.name, s1.marks) = > ये print() function screen पर s1 object से name और marks variables को display करता है |


    Advantages Of Instance Variables

    1. Creation = > Instance Variables को Object के according create किया जाता है Numbers of Variables fix नही है |
    2. use = > Instance Variables का उपयोग करने के लिए self Keyword का उपयोग किया जाता है |
    3. Data = > Instance Variables Object data को store करते है |
    4. decorator = > Class Variables के जैसे Instance Variables मे किसी decorator का उपयोग नही किया जाता है |
    5. Object information = > Instance Variables Object के according Data Store करते है |


    Diffrence Bitween Instance and Class Variables

    Base Class Variables Instance Variables
    Value Assignment Variables create करते Time Value set की जाती है | Object create करते Time Value set की जाती है |
    Use Class Variables का उपयोग अधिक किया जाता है | Instance Variables का उपयोग कम किया जाता है |
    Access class और object दोनों से access किए जा सकते है | केवल Object से access किए जाते है |
    Memory Class Variables कम Computer Memory का उपयोग करते है | Instance Variables अधिक Computer Memory का उपयोग करते है |
    Copies class के create किए गए सभी Object use कर सकते है | Instance variables को class के हर object के according create किया जाता है |

    Magic Methods

    Magic Methods OOPs का महत्वपूर्ण Concept है जिसके उपयोग से Programs की Lenght कम होती है बडे - बडे Logics छोटे Logics मे convert हो जाते है | Magic Methods को Double Under score से define किया जाता है | Magic Methods को अलग से object बना करके call करने की जरूरत नही होती है | Magic Methods को Dunder Methods के नाम से भी जाना जाता है | __init__ , __str__ , __add__ etc. Magic Methods के Examples है | Magic Methods को Object Behavior customize करने के लिए उपयोग किया जाता है | नीचे कुछ Dunder Methods दिए गए है |
    __str__ = > एक Built In Magic Method है जिसका उपयोग string के लिए किया जाता है | print करने पर Screen पर कया show होगा यह तय करने के लिए __str__ method का उपयोग किया जाता है क्योंकि यह एक Magic Method है , इसलिए इसको अलग से class Object बना के call नही किया जाता है | ये अपने आप run हो जाता है | नीचे __str__ Magic Method का एक Code दिया गया है |

    1class Student:
    2    def __init__(self, name):
    3        self.name = name
    4    def __str__(self):
    5        return f"Student name is {self.name}"
    6s1 = Student("Amit")
    7print(s1)
        

    Code Explanation

    class Student: = > एक Student class create की गई है |
    def __init__(self, name): = > एक constuctor Function Create किया गया है , जिसमे name parameter दिया गया है |
    self.name = name = > name variable की value function के name variable के बराबर set की गई है |
    def __str__(self): = > एक string function create किया गया है |
    return f"{self.name}" = > ये print() function output मे name variable को display करता है |
    s1 = Student("Amit") = > Student class का s1 नाम का एक object create किया गया है और name variable मे "Amit" store किया गया है |


    __add__() = > OOPs मे __add__() Magic Method operator overriding का एक concept है जिसको double under score add से define किया जाता है | यह एक Magic Dunder का Method है जो + Operator के Behavior को define करता है | यह Method बिलकुल Addition(+) के जैसे ही काम करता है | नीचे __add__ Magic method का code दिया गया है |

    1class Number:
    2    def __init__(self, value):
    3        self.value = value
    4    def __add__(self, other):
    5        return self.value + other.value
    6n1 = Number(10)
    7n2 = Number(20)
    8print(n1 + n2) 
        

    Code Explanation

    class Number: = > Number name की एक Class Create की गई है |
    def __init__(self, value): = > एक constuctor Function create किया गया है , जिसमे value variable create किया गया है |
    self.value = value = > value variable की value function के value variable के Equal set की गई है |
    def __add__(self, other): = > एक addition function create किया गया है , जिसमे other variable create किया गया है |
    return self.value + other.value = > ये return statement value और other variable के addition value को return करती है |
    n1 = Number(10) = > Number class का n1 नाम का object create किया गया है |
    print(n1 + n2) = > ये print() function n1 और n2 की addition value को show करता है |


    __eq__ = > __eq__ एक Magic Method है जो == operator के Behavior को define करता है | __eq__ Method को __add__ , __str__ Method के जैसे Define नही किया जाता है | __eq__ Method के उपयोग के लिए == operator का उपयोग किया जाता है | __eq__ Method Output मे True या False देता है | नीचे __eq__ Method का code दिया गया है |

    1class Student:
    2    def __init__(self, name):
    3        self.name = name
    4s1 = Student("Rahul")
    5s2 = Student("Rahul")
    6print(s1 == s2) 
        

    Code Explanation

    class Student = > Student नाम की एक Class create की गई है |
    def __init__(self, name): = > एक constuctor function create किया गया है , जिसमे name parameter pass किया गया है |
    self.name = name = > name variable create किया गया है जिसकी value function के name variable के बराबर है |
    s1 = Student("Rahul") = > Student class का s1 नाम का object create किया गया है और name variable मे "Rahul" Name store किया गया है |
    print(s1 == s2) = > ये print() function s1 और s2 variable के Comparison को show करता है |


    Access Modifiers क्या होते हैं ?

    Python Language हो या C , C++ , Java जैसी कोई दूसरी Language सब मे Access Modifiers का काम बिलकुल एक जैसा होता है Programming Language के अनुसार Access Modifiers का Code change हो जाता है | Access Modifiers का उपयोग Create की गई Class मे stored Data को secure करने के लिए किया जाता है | Access Modifiers से मतलब है Create की गई Class मे Variables , Methods , Functions etc. को access करने के बहुत तरीके | Access Modifiers Class को access करने के तरीको Control करते है | Python Language मे Access Modifiers को निम्नलिखित तीन Parts मे Devide किया गया है |

    1. Public Method

    जब हम कोई class create करते है तो class मे create किए गए function , methods , variables etc. default मे Public ही होते है , इसलिए इनको create करने के लिए किसी special Keyword का उपयोग नही किया जाता है और इस तरह से stored data को कोई भी access कर सकता है | नीचे Public Modifier से संबंधित एक Python Code दिया गया है |

    1class Student:
    2    def __init__(self, name):
    3        self.name = name   
    4    def display(self):
    5        print("Name:", self.name)
    6s1 = Student("Rahul")
    7print(s1.name)
    8s1.display() 
        

    Code Explanation

    Class Student = > एक Student नाम की Class create की गई है |
    def __init__(self, name): = > एक constuctor function create किया गया है , जिसमे name नाम का parameter भी pass किया गया है |
    self.name = name = > एक Public name variable create किया गया है जिसकी value function के name variable के बराबर है |
    def display(self): = > display नाम का एक function बनाया गया है |
    print("Name:", self.name) = > ये print() function name variable को display करता है |
    s1 = Student("Rahul") = > Student class का s1 नाम का एक Function create किया गया है और name variable मे "Rahul" Name store किया गया है |
    print(s1.name) = > ये print() function name variable को show करता है |
    s1.display() = > s1 object से display function को run किया गया है |


    Advantages Of Public Modifier

    1. Access = > Public Methods को Program मे कही से भी Access किया जा सकता है जिससे की अधिक Codes नही लिखने की जरूरत होती |
    2. Creation = > C++ जैसी Language के जैसे Python मे Public methods को Create करने के लिए किसी Keyword या Method की जरूरत नही है |
    3. Uses = > Public Methods को जब Create करने का कोई Method या Keyword नही है तो इनको access करने के लिए भी किसी Keyword , Method etc. की जरूरत नही होती है |
    4. Beginer Friendly = > Public Modifier के Concept को समझना Beginer के लिए के लिए सरल है |
    5. Program Clean = > Public Modifier से data को access करना Create करपाना सरल होने के कारण लिखे जा रहे Programs Clean होते है और सरलता से समझ आने वाले होते है |


    2. Protected Modifier

    Protected Modifier का काम Public Modifier के जैसे ही होता है , लेकिन Structure , uses , Security मे ये Public Modifier से advance है | Protected Modifier default मे Public Modifier के जैसे पहले से ही Set नही होते है इनके उपयोग के लिए इनको अलग से create करना पडता है | Protected Modifier से मतलब इनमे data Privately store होता है पर access publicly किया जाता है | Protected Modifier के उपयोग के लिए single underscore (_) का उपयोग किया जाता है | नीचे Protected Modifier का एक Code दिया गया है |

    1class Student:
    2    def __init__(self, name):
    3        self._name = name   
    4class Child(Student):
    5    def show(self):
    6        print("Name:", self._name)
    7c = Child("Amit")
    8c.show()
        

    Code Explanation

    class Student : = > Student नाम की एक Class create की गई है |
    def __init__(self, name): = > एक constuctor function create किया गया है |
    self._name = name = > एक name नाम का Protected variable create किया गया है जिसकी value function के name variable के बराबर है |
    class Child(Student): = > एक Child नाम की class Create की गई है और इस Child class मे Student class को inherit किया गया है |
    def show(self): = > show नाम का एक Function create किया गया है |
    print("Name:", self._name) = > ये print() function name variable को show करता है |
    c = Child("Amit") = > child class का c नाम का एक object create किया गया है |
    c.show() = > c object से show() function को call की गई है |


    Advantages Of Protected Modifiers

    1. Creation = > Python Language मे Protected Modifiers को create करने के लिए single underscore (_) का उपयोग किया जाता है |
    2. Data Security = > Public Modifiers के Comparison मे Protected Modifiers मे data अधिक Secure होता है |
    3. Access = > Protected Modifiers को create करने के लिए तो (_) का लेकिन access करने के लिए symbol या Keyword नही है |
    4. Class के बाहर से Access = > Protected Modifiers को Class के बाहर से भी Access किया जा सकता है |


    3. Private Modifier

    Private Modifier speed , uses , Security सभी मे Public और Protected Modifiers से advance है | Private Modifiers से मतलब है इस Modifiers से data पूरी तरह से secure होता है उसको कोई भी अनजान person access नही कर सकता है | Private Modifiers के उपयोग के लिए Python मे double underscore (__) का उपयोग किया जाता है | नीचे Private Modifier का एक code दिया गया है |

    1class Student:
    2    def __init__(self, name):
    3        self.__name = name 
    4    def display(self):
    5        print("Name:", self.__name)
    6s = Student("Rohit")
    7s.display() 
        

    Code Explanation

    Class Student = > एक Student Name की class create की गई है |
    def __init__(self, name): = > एक constuctor function create किया गया है , जिसमे name variable create किया गया है |
    self.__name = name = > name नाम का एक Private variable create किया गया है |
    def display(self): = > display नाम का एक function create किया गया है |
    print("Name:", self.__name) = > ये print() function private name variable को display करता है |
    s = Student("Rohit") = > Student class का एक s नाम का object create किया गया है और name variable मे "Rohit" name Store किया गया है |
    s.display() = > s object से display function को call की गई है |


    Advantages Of Private Modifier

    1. Creation = > Private Modifiers variables को create करने के लिए double underscore(__) का उपयोग किया जाता है |
    2. Access = > Private Modifiers को access करने के लिए किसी भी Keyword का उपयोग नही किया जाता है |
    3. Security = > Private Modifiers मे Stored data को कोई भी access नही कर सकता है |

    You have completed This Chapter ! 🎉

    Now you can choose any one of the following options to test your knowledge :

    💻

    Code Practice

    Solve practical exercises .

    Practice Now
    🏆

    Knowledge Test

    Answer the questions of the chapter .

    Start Test
    📘

    Next Chapter

    Continue your Python journey by reading the next chapter.

    Next Chapter
    🆘

    Help Center

    Users and we all help you together .

    Peer Learning
    Expert Advice
    Live Chat
    Fast Solutions