bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

 

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!

Previous Next

Download Interview Questions asked by top MNCs in 2018?

Learn SQL in 16 hrs from experts



"0 Responses on Python Classes"

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python Classes.