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:

  • Inheritance
  • Composition
  • Multiple instances
  • Customization via inheritance
  • Operator overloading

 
Create Classes
Keyword class is used to create the class. The basic structure of class is –

class <class_name> :
# Definition of class

 
Create Instance Objects
e.g.

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

e.g.

st1 = Student (1,”abc”)
st2 = Student (2, ”pqr”)
st2 = Student (3, ”xyz”)
 

Access Attributes
Dot is used to access the attributes.

st1.showstudent()
st2.showstudent()
st3.showstudent()
print "Total Students are %d" % Student.s1

Output
ID: 1, abc
ID: 2, pqr
ID: 3, xyz
Total Students are 3
Learn more about PYTHON TOOLKIT in this insightful blog now!

Leave a Reply

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

Solve : *
22 + 26 =