Class is a prototype for an object which defines a collection of attributes and these attributes are variable and methods which are accessed by dot. Class provides many features like:
- Multiple instances
- Customization via inheritance
- Operator overloading
Keyword class is used to create the class. The basic structure of class is –
class <class_name> : # Definition of class
Create Instance Objects
class Student: s1 = 0 def __init__(self, id, name): self.id = id self.name = name Student.s1+=1 def showcount(self): print "Total students are %d" % student.s1 def showstudent(self): print "ID : ", self.id, ", Name: ", self.name
st1 = Student (1,”abc”) st2 = Student (2, ”pqr”) st2 = Student (3, ”xyz”)
Dot is used to access the attributes.
st1.showstudent() st2.showstudent() st3.showstudent() print "Total Students are %d" % Student.s1
ID: 1, abc
ID: 2, pqr
ID: 3, xyz
Total Students are 3
Learn more about PYTHON TOOLKIT in this insightful blog now!