Python Classes और Objects
Class attributes और methods का blueprint है; object उसका एक concrete instance होता है।
Classes और Objects क्या है?
Class attributes और methods का blueprint है; object उसका एक concrete instance होता है।
State और behavior वाला अपना type बनाएँ।
इसका उपयोग कब करें?
- चलने योग्य code से Python syntax सीखें।
- बड़ी programming समस्याओं से पहले मजबूत आधार बनाएँ।
- स्थानीय setup के बिना किसी विचार को तुरंत जाँचें।
उदाहरण कोड
main.py
class Course:
def __init__(self, name, lessons):
self.name = name
self.lessons = lessons
def describe(self):
return f"{self.name}: {self.lessons} lessons"
course = Course("Python Basics", 12)
print(course.describe())
अपेक्षित आउटपुट
Python Basics: 12 lessons
यह कैसे काम करता है
__init__ initial state सेट करता है और self वर्तमान instance के attributes तक पहुँच देता है।
मान बदलें और Python इंस्टॉल किए बिना CodeUtility ऑनलाइन Python कंपाइलर में प्रोग्राम चलाएँ।
अभ्यास के कार्य
बड़े dataset पर जाने से पहले input बदलें और edge cases की जाँच करें।
- Code चलाने से पहले output का अनुमान लगाएँ।
- खाली और गलत input संभालें।
- Logic को function में रखें और tests जोड़ें।