Object Oriented Programming Classes and Methods in Python

Object Oriented Programming Classes and Methods in Python

Python programming also supports object-oriented classes and methods.

Syntax:

class ClassName:
    def __init__( self, variable1, variable2, ... ):
    def methodName( self ):

Syntax to instantiate:

obj = ClassName( variable value1, .... );

Syntaxt to call the method:

obj.methodName();

Sample Code:

class Employee:
    def __init__( self, empNo, empFirstName, empLastName ):
        self.empNo = empNo;
        self.empFirstName = empFirstName;
        self.empLastName = empLastName;
    def getEmployeeFullName( self ):
        return self.empFirstName + ' ' + self.empLastName;
objEmp = Employee( 123, 'Test 1', 'Test 2' );
print(
    objEmp.getEmployeeFullName()
);

Leave a Reply