OCJP

Class and Objects in Python3

We can create classes in Python like:

class Student:
    #def __init__(self):
    #    self.nm="Jadeja"
    #    self.roll_no=34

    def __init__(self,nm="Jadeja",roll=24):
        self.nm=nm
        self.roll_no=roll
    def show(self):
        print(self.nm)

S=  Student()
S.show()

s1= Student("ABX",32)
s1.show()

Here __init() works as constructor and self like ‘this’

Output

Jadeja
ABX
>>>

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?