पायथन ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग

इस ट्यूटोरियल में, आप पायथन में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (ओओपी) और उदाहरणों की मदद से इसकी मूलभूत अवधारणा के बारे में जानेंगे।

वीडियो: पायथन में ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग

वस्तु उन्मुख कार्यकर्म

पायथन एक बहु-प्रतिमान प्रोग्रामिंग भाषा है। यह विभिन्न प्रोग्रामिंग दृष्टिकोणों का समर्थन करता है।

प्रोग्रामिंग समस्या को हल करने के लिए लोकप्रिय दृष्टिकोणों में से एक ऑब्जेक्ट बनाना है। इसे ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP) के रूप में जाना जाता है।

एक वस्तु की दो विशेषताएँ होती हैं:

  • विशेषताएँ
  • व्यवहार

आइए एक उदाहरण लेते हैं:

एक तोता एक वस्तु हो सकता है, क्योंकि इसमें निम्नलिखित गुण हैं:

  • नाम, उम्र, रंग विशेषताओं के रूप में
  • गायन, व्यवहार के रूप में नृत्य

पायथन में ओओपी की अवधारणा पुन: प्रयोज्य कोड बनाने पर केंद्रित है। इस अवधारणा को DRY (अपने आप को दोहराएं नहीं) के रूप में भी जाना जाता है।

पायथन में, OOP की अवधारणा कुछ बुनियादी सिद्धांतों का अनुसरण करती है:

कक्षा

एक वर्ग वस्तु के लिए एक खाका है।

हम कक्षा को लेबल के साथ तोते के एक स्केच के रूप में सोच सकते हैं। इसमें नाम, रंग, आकार आदि के बारे में सभी विवरण शामिल हैं। इन विवरणों के आधार पर, हम तोते के बारे में अध्ययन कर सकते हैं। यहां, एक तोता एक वस्तु है।

तोते के वर्ग के लिए उदाहरण हो सकता है:

 कक्षा तोता: पास

यहां, हम classएक खाली क्लास पैरट को परिभाषित करने के लिए कीवर्ड का उपयोग करते हैं । वर्ग से, हम उदाहरणों का निर्माण करते हैं। एक उदाहरण एक विशेष वर्ग से निर्मित एक विशिष्ट वस्तु है।

वस्तु

एक वस्तु (उदाहरण) एक वर्ग का एक तात्कालिकता है। जब वर्ग को परिभाषित किया जाता है, तो केवल वस्तु के लिए विवरण परिभाषित किया जाता है। इसलिए, कोई मेमोरी या भंडारण आवंटित नहीं किया जाता है।

तोता वर्ग की वस्तु के लिए उदाहरण हो सकता है:

 ओब्ज = तोता ()

यहाँ, obj वर्ग की एक वस्तु है Parrot

मान लीजिए कि हमारे पास तोतों का विवरण है। अब, हम यह दिखाने जा रहे हैं कि तोते की कक्षा और वस्तुओं का निर्माण कैसे किया जाए।

उदाहरण 1: पायथन में क्लास और ऑब्जेक्ट बनाना

 class Parrot: # class attribute species = "bird" # instance attribute def __init__(self, name, age): self.name = name self.age = age # instantiate the Parrot class blu = Parrot("Blu", 10) woo = Parrot("Woo", 15) # access the class attributes print("Blu is a ()".format(blu.__class__.species)) print("Woo is also a ()".format(woo.__class__.species)) # access the instance attributes print("() is () years old".format( blu.name, blu.age)) print("() is () years old".format( woo.name, woo.age))

आउटपुट

 ब्लू एक पक्षी है वू भी एक पक्षी है ब्लू 10 साल का है वू 15 साल का है

उपरोक्त कार्यक्रम में, हमने तोता नाम के साथ एक वर्ग बनाया। फिर, हम विशेषताओं को परिभाषित करते हैं। विशेषताएँ किसी वस्तु की विशेषता होती हैं।

इन विशेषताओं को __init__वर्ग की विधि के अंदर परिभाषित किया गया है । यह इनिशियलाइज़र तरीका है जो ऑब्जेक्ट बनते ही सबसे पहले चलता है।

फिर, हम तोता वर्ग के उदाहरण बनाते हैं। यहाँ, ब्लू और वू हमारी नई वस्तुओं के संदर्भ (मूल्य) हैं।

हम कक्षा विशेषता का उपयोग करके पहुंच सकते हैं __class__.species। कक्षा के सभी उदाहरणों के लिए कक्षा की विशेषताएँ समान हैं। इसी तरह, हम इंस्टेंस विशेषताओं का उपयोग करके एक्सेस करते हैं blu.nameऔर blu.age। हालाँकि, श्रेणी के हर उदाहरण के लिए उदाहरण विशेषताएँ भिन्न होती हैं।

कक्षाओं और वस्तुओं के बारे में अधिक जानने के लिए, अजगर कक्षाओं और वस्तुओं पर जाएं

तरीके

विधि एक वर्ग के शरीर के अंदर परिभाषित कार्य हैं। उनका उपयोग किसी वस्तु के व्यवहार को परिभाषित करने के लिए किया जाता है।

उदाहरण 2: पायथन में तरीके बनाना

 class Parrot: # instance attributes def __init__(self, name, age): self.name = name self.age = age # instance method def sing(self, song): return "() sings ()".format(self.name, song) def dance(self): return "() is now dancing".format(self.name) # instantiate the object blu = Parrot("Blu", 10) # call our instance methods print(blu.sing("'Happy'")) print(blu.dance())

आउटपुट

 ब्लू 'हैप्पी ’गाना अब ब्लू डांस कर रहा है

उपरोक्त कार्यक्रम में, हम दो तरीकों को परिभाषित करते हैं जैसे कि sing()और dance()। इन्हें इंस्टेंस मेथड्स कहा जाता है क्योंकि इन्हें एक इंस्टेंस ऑब्जेक्ट पर कहा जाता है blu

वंशानुक्रम

Inheritance is a way of creating a new class for using details of an 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).

Example 3: Use of Inheritance in Python

 # parent class class Bird: def __init__(self): print("Bird is ready") def whoisThis(self): print("Bird") def swim(self): print("Swim faster") # child class class Penguin(Bird): def __init__(self): # call super() function super().__init__() print("Penguin is ready") def whoisThis(self): print("Penguin") def run(self): print("Run faster") peggy = Penguin() peggy.whoisThis() peggy.swim() peggy.run()

Output

 Bird is ready Penguin is ready Penguin Swim faster Run faster

In the above program, we created two classes i.e. Bird (parent class) and Penguin (child class). The child class inherits the functions of parent class. We can see this from the swim() method.

Again, the child class modified the behavior of the parent class. We can see this from the whoisThis() method. Furthermore, we extend the functions of the parent class, by creating a new run() method.

Additionally, we use the super() function inside the __init__() method. This allows us to run the __init__() method of the parent class inside the child class.

Encapsulation

Using OOP in Python, we can restrict access to methods and variables. This prevents data from direct modification which is called encapsulation. In Python, we denote private attributes using underscore as the prefix i.e single _ or double __.

Example 4: Data Encapsulation in Python

 class Computer: def __init__(self): self.__maxprice = 900 def sell(self): print("Selling Price: ()".format(self.__maxprice)) def setMaxPrice(self, price): self.__maxprice = price c = Computer() c.sell() # change the price c.__maxprice = 1000 c.sell() # using setter function c.setMaxPrice(1000) c.sell()

Output

 Selling Price: 900 Selling Price: 900 Selling Price: 1000

In the above program, we defined a Computer class.

We used __init__() method to store the maximum selling price of Computer. We tried to modify the price. However, we can't change it because Python treats the __maxprice as private attributes.

As shown, to change the value, we have to use a setter function i.e setMaxPrice() which takes price as a parameter.

Polymorphism

Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data types).

Suppose, we need to color a shape, there are multiple shape options (rectangle, square, circle). However we could use the same method to color any shape. This concept is called Polymorphism.

Example 5: Using Polymorphism in Python

 class Parrot: def fly(self): print("Parrot can fly") def swim(self): print("Parrot can't swim") class Penguin: def fly(self): print("Penguin can't fly") def swim(self): print("Penguin can swim") # common interface def flying_test(bird): bird.fly() #instantiate objects blu = Parrot() peggy = Penguin() # passing the object flying_test(blu) flying_test(peggy)

Output

 Parrot can fly Penguin can't fly

In the above program, we defined two classes Parrot and Penguin. Each of them have a common fly() method. However, their functions are different.

बहुरूपता का उपयोग करने के लिए, हमने एक सामान्य इंटरफ़ेस अर्थात flying_test()फ़ंक्शन बनाया जो किसी भी ऑब्जेक्ट को लेता है और ऑब्जेक्ट की fly()विधि को कॉल करता है । इस प्रकार, जब हमने flying_test()फंक्शन में ब्ल्यू और पैगी ऑब्जेक्ट्स को पास किया , तो यह प्रभावी रूप से चला।

याद रखने के लिए महत्वपूर्ण बिंदु:

  • ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग प्रोग्राम को समझने के साथ-साथ कुशल भी बनाता है।
  • चूँकि कक्षा में परिवर्तन होता है, इसलिए कोड का पुन: उपयोग किया जा सकता है।
  • डेटा एब्सट्रैक्ट के साथ डेटा सुरक्षित और सुरक्षित है।
  • बहुरूपता विभिन्न वस्तुओं के लिए एक ही इंटरफ़ेस की अनुमति देता है, इसलिए प्रोग्रामर कुशल कोड लिख सकते हैं।

दिलचस्प लेख...