If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
पिछले topics में आपने Python में classes के बारे में पढ़ा इस topic में आप class constructor के बारे में पढ़ेंगे।
class में एक __init__() function होता है , जो class का object बनने पर automatically run होता है। इस constructor कहते हैं। हालाँकि class में define करें ये जरूरी नहीं होता है।
class myClass :
def __init__(self) :
print('Object initialized')
myobj = myClass()
C:\Users\Rahulkumar\Desktop\python>python oop_ex.py Object initialized
जैसे कि example में आप देख सकते हैं कि __init__() function को कही भी call नहीं किया गया है , object create होते ही यह automatically call हुआ है।
Python में defined हर एक function में एक by default parameter pass होता है , जो कि current class object होता है। इसकी help से हम class के अंदर सभी class variables और functions को access करते हैं।
example में आप देख सकते हैं , function में self name का parameter लिया गया है। हालाँकि आप किसी भी name का variable ले सकते हैं।
__init__() की help से आप class का object बनाते समय variables pass कर सकते हैं और उन्हें class में use कर सकते हैं।
See Example -
class myClass :
def __init__(self, name) :
print('Hello : ', name)
myobj = myClass("Rahul Kuamr")
C:\Users\Rahulkumar\Desktop\python>python oop_ex.py Hello : Rahul Kuamr
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook