Passing Arguments in Class in Python

Passing arguments to a function of a class in Python programming using object of that class. It is user-defined data type which combines data and methods for manipulating that data into one package.

Class Program Passing Arguments

Program

class cls: #'cls' is name of Class.
    def fun(self):
        print("This is Simple Function")
    def fun2(self, name, roll, sec): #passing here arguments.
        print("My Name is: ", name) #and printing.
        print("My Roll no is: ", roll)
        print("My Section is: ", sec)
obj = cls() #'obj' is object of class 'cls'.
obj.fun()
obj.fun2("HiLaLs Afirdi", 12012, 'B') #calling and passing arguments.
Output
This is Simple Function
My Name is: HiLaLs Afirdi
My Roll no is: 12012
My Section is: B

Class Program Passing Arguments Another Method

Program

class cls: #'cls' is name of Class.
    def fun(self):
        print("This is Simple Function")
    def fun2(self, name, roll, sec): #passing here arguments.
        print("My Name is: ", name) #and printing.
        print("My Roll no is: ", roll)
        print("My Section is: ", sec)
obj = cls() #'obj' is object of class 'cls'.
obj.fun()
obj.fun2("HiLaLs Afirdi", 120122067, 'B') #calling and passing arguments.
print("------------------------")
obj.fun2("Haq Nawaz", 120122, 'A') #calling and passing arguments.
print("------------------------")
obj.fun2("Amir", 12012, 'A') #calling and passing arguments.
Output
This is Simple Function
My Name is: HiLaLs Afirdi
My Roll no is: 120122067
My Section is: B
------------------------
My Name is: Haq Nawaz
My Roll no is: 120122
My Section is: A
------------------------
My Name is: Amir
My Roll no is: 12012
My Section is: A

Class Program Calling and Passing Arguments

Program

class cls: #'cls' is name of Class.
    def fun(self):
        print("This is Simple Function")
    def set(self, name, roll, sec): #passing here arguments.
        self.n = name #assigning variables values to another variables.
        self.r = roll
        self.s = sec
    def get(self):
        print("My Name is: ", self.n) #and printing.
        print("My Roll no is: ", self.r)
        print("My Section is: ", self.s)

obj = cls() #'obj' is object of class 'cls'.
obj.fun()
obj.set("HiLaLs Afirdi", 12012, 'B') #calling and passing arguments.
obj.get()
Output
This is Simple Function
My Name is: HiLaLs Afirdi
My Roll no is: 12012
My Section is: B

Class Program Assigning Values to Variables
Program

class cls: #'cls' is name of class.
    def fun(self):
        print("First Name is: ", self.fname)
        print("Last Name is: ", self.lname)
        print("Roll No is: ", self.roll)
x = cls() #'x' is object of class 'cls'.
x.fname = "HiLaLs" #with the help of object 'x' assigning
x.lname = "Afridi" #values to variables.
x.roll = 12012
x.fun() #calling 'fun()' function.
Output
First Name is: HiLaLs
Last Name is: Afridi
Roll No is: 12012

Program Passing Arguments and Printing Return Value
Program

class cls:
    def fun(self, a, b): #passing here arguments.
        c = a + b #adding 'a' and 'b' value and assigning to 'c'.
        return c #returning 'c' value.
obj = cls() #'obj' is object of class 'cls'.
val = obj.fun(5, 6) #calling and passing arguments to parameters and assigning
print("Return Value is: ", val) #Return value to 'val' and printing.
Output
Return Value is: 11