Python Classes and Objects Overview

Python ek completely object oriented programming language. Jisme class aur object ke features ise fully functional banate hain. Simple si language me kaha jaye to class ek blueprint hota hai aur wahi object uss blueprint ke according apne sare works complete karta hai. 

Class Methods aur Properties ka collection hai aur jaise hi uss class ka object create hota hai waise hi wah object uss class ki sare properties aur methods ko access kar sakta hai.

Class tab tak koi bhi memory storage nhi karega jab tak uske sath kisi object ko associate na kiya jaye. Dusri language me kahen to class ek object constructor hota hai.

Class ko kaise create karen?

Python me kisi bhi class ko create karne ke liye “class” keyword ka use kiya jata hai. Example dekhiye:

class myClass():
    y = 5

Object ko kaise create karen?

Ab hum myClass() ke object(p1) ko create karenge. Jisse hum ‘y’ value ka output print karayege jo myClass() ki ek property hai.

Ab hum myClass() ke object(p1) ko create karenge. Jisse hum ‘y’ value ka output print karayege jo myClass() ki ek property hai.

Python me __init__() function ka kya role hota hai?

Python oops ke concept me __init__() function ka bahut important role hota hai. Yah python ka ek built-in function hai.

Har ek class ka __init__() function hota hai aur jaise hi kisi class ka koi object create hota hai yah __init__() function apne aap execute ho jata hai. 

Aur execute hote hi yah function class ke associated object ki sari properties ko assign kar deta hai. Example se samajhte hain:__init__()  (… init ke pahle aur baad be double underscore)

class Person:
        def __init__(self,name,age):
            self.name = name
            self.age = age
        def myfoo(self):
            print(“My name is ” + self.name)

o1 = Person(“Rakesh”, 35)

print(o1.name)
print(o1.age)
print(o1.myfoo())

Upar diye gaye example me Person naam ki ek class hai jaha __init__() uss class ka method hai. o1 ek object hai aur iss object ke through hum class ke properties aur methods ko use kar rahen hain.

def __init__(self,name,age) me self class ka ek instance hai jo class me defined attributes aur methods ko access kar sakta hai. Aur yahi self keyword object se associated information(“Rakesh”,35) ko __init__() function me present arguments (name, age) ke sath associate karega.

***self ka naam kuch bhi rakh sakte hain jaise “ram”, “shyam”,

Wahi dusri tarah __init__() function kisi bhi class ka ek reserved method hota hai, jo class ke case me ek constructor bhi kahlata hai. Jiska important role yah hai ki wah object ke create hote hi class ke attributes ko  activate karta hai.

Final Touch:
To jaise hi object create hoga waise hi __init__() function chalega aur self keyword present arguments(name,age) ke sath object ke through bheje gaye information ko assign kar dega, aur jaise hi print function call hoga waise hi __init__() function output bhi print kar dega.

***instance means ek object jo kisi particular class ke properties aur methods ko follow karta hai…

Upar diye gaye code me myfoo() Person class ka ek function hai, jise method bhi kaha jata hai. Iss method ko object ke through bhi call kiya jata hai. jaise o1.myfoo() 

Python class me object ke sath manipulations

OBJECT KI PROPERTIES KA MODIFICATION 

Before Modification : o1.Person(“Rakesh”,35)
After Modification: o1.age = 54

OBJECT KO DELETE KARNA (using del Keyword)

del o1                          # poore object ka deletion
del o1.name               # object ki properties ka deletion 

Class me Pass statement ka use

Class me bhi pass statement ka use usi tarah kiya jata hai jaise functions ke case me kiya jata hai. Agar kisi class me content nhi hai to simply pass statement add kar dijiye, jisse errors generate nhi hogi. 

class Book:
    pass