Python Class and Object

Python is a object-oriented programming Language, which is used to implement all OOP concept at the programming level.

Python class is a

  1. Template or a Blue Print
  2. Contains Variables, Methods and Constructors
  3. Variable shows the object Properties and Methods shows Actions performed by the object.

Syntax:
class <classname>:
// class members

Example: 1
# class and objects in python

#class declaration
class Emp:
# method
def assign(self):
self.empno=2008
self.ename=’Sanjay’

def showEmpInfo(self):
print (“Employee Details”)
print (“Employee no : “, self.empno)
print(“Employee name : “,self.ename)

#object creation
objE=Emp()
#calling the method using object
objE.assign()
objE.showEmpInfo()

Example: 2

#class declaration
class Emp:
# method declaration
def assign1(self,empno, empname):
self.empno=empno
self.empname=empname

def showEmpInfo(self):
print (“Employee Details”)
print (“Employee no : “, self.empno)
print(“Employee name : “, self.empname)

#object creation
objE1=Emp()
objE2=Emp()

print(“Example self keyword in python class”)
#calling the method using object
objE1.assign1(2009, “Vijaya”)
objE1.showEmpInfo()

objE2.assign1(2004, “Sanjay”)
objE2.showEmpInfo()

Win Corporate Training
website: http://www.wincorptrg.com
View My Profile

Advertisements