codescracker


python

Python Classes and Objects



« Previous Tutorial Next Tutorial »


To create a new class definition in python, use the class statement. Here is the general form to define a class in python:

class class_name:
	'class docstring'
	class_suite

Here, class has a documentation string (docstring) that can be accessed via the class_name._doc_, and the class_suite basically consists of all the component statements defining the class members, data attributes and functions.

Let's take a look at this example, defining a class, STUDENT, in python:

class STUDENT:
   'Common base class for all employees'
   totalStudent = 0

   def __init__(self, name, fee):
      self.name = name
      self.fee = fee
      STUDENT.totalStudent = STUDENT.totalStudent + 1
   
   def displayCount(self):
     print("Total Student = %d" % STUDENT.totalStudent)

   def displayStudent(self):
      print("Name:", self.name,  ", Fee:", self.fee)

Here, the variable totalStudent is a class variable whose value is shared among all the instances of this class. This can be simply accessed as STUDENT.totalStudent from inside the class or from outside the class. The method _init_() is a special method, called as class constructor or initialization method that python calls when you create a new instance of this class. You are free to declare other class methods just like normal functions in python. Python adds the self argument to the list for you, and you don't need to include it when you call the methods

Create Instance Objects in Python

To create an instance of a class, you simply call the class using the class name and then pass in whatever arguments its _init_ method accepts. Here is an example.

stud1 = STUDENT("Devraj", 2500)	# created first object of STUDENT class
stud2 = STUDENT("Alok", 2150)	# created second object of STUDENT class
stud3 = STUDENT("Satyam", 3200)	# created third object of STUDENT class

Access Attributes in Python

You can access the attributes of the object just by using the dot (.) operator with object. Class variable can be access by using the class name. Here is an example.

stud1.displayStudent()
stud2.displayStudent()
stud3.displayStudent()
print("Total Student = %d" % STUDENT.totalStudent)

Python Classes and Objects Example

Here is an example program, demonstrating the concept and use of classes and objects in python. This is the complete version of the above python codes:

# Python Classes and Objects - Example Program

class STUDENT:
   'Common base class for all employees'
   totalStudent = 0

   def __init__(self, name, fee):
      self.name = name
      self.fee = fee
      STUDENT.totalStudent = STUDENT.totalStudent + 1
   
   def displayCount(self):
     print("Total Student = %d" % STUDENT.totalStudent)

   def displayStudent(self):
      print("Name:", self.name,  ", Fee:", self.fee)

stud1 = STUDENT("Devraj", 2500)
stud2 = STUDENT("Alok", 2150)
stud3 = STUDENT("Satyam", 3200)
stud1.displayStudent()
stud2.displayStudent()
stud3.displayStudent()
print("Total Student = %d" % STUDENT.totalStudent)

Here is the sample output produced by the above python program:

python classes and objects

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test