Class and Object in Python

 Object Oriented Programming with Python


Python supports to OOP concepts like classes, objects, polymorphism, encapsulation, inheritance and data abstraction. Properties and behaviours are bundled together into objects. 

For example,

A person with properties name, age, address, contact number and behaviour like reading, writing, watching, laughing, walking, talking etc.  

Defining Class in Python:

A class is a user defined data type which is a blueprint of object. It is collection of objects. Class contains some attributes and methods. Attributes are nothing but variables of class. 

Syntax to define class,

class class_name:

statement1

statement2

........

statementn

Class is defined using class keyword. class_name is user defined name followed by colon(:). Statement1, statementn are related processing statements to perform operation on class.  

For Example,

class person:

    name="My name is"

    print(name)

O/P:

My name is

In above example person is name of class with name is its property.

Another example

class addition:

    a=b=20

    print("Addition of two numbers is:",a+b)

O/P:

Addition of two numbers is: 40

In above example class is defined with class name addition with two properties a and b.

Creating Objects:

Object is an instance of class. It is a variable of class. 

Syntax to declare Object:

obj=class_name()

obj is name of object. 

Example,


class myname:

    name=" Manisha More"

    obj=myname()

    print(obj.name)

O/P:

 Manisha More


Another Example,

class addition:

    a=b=20

    sum1=a+b

    obj=addition()

    print("Addition of two numbers is:",obj.sum1)

O/P:

Addition of two numbers is: 40


A program to perform Arithmetic operations using class and object in python

# Addition of Two Numbers using class and object

a=int(input("Enter first number:\t"))

b=int(input("Enter second number:\t"))

# class definition

class arith:

    add=a+b

    sub=a-b

    mult=a*b

    div=a/b 

    mod=a%b

# object creation

obj=arith()

print("Arithmetic Operations are:\t")

print("Addition is:\t",obj.add)

print("Subtraction is:\t",obj.sub)

print("The multiplication is:\t",obj.mult)

print("Division is:\t",obj.div)

print("The remainder is:\t",obj.mod)

O/P:

Enter first number:     20

Enter second number:    20

Arithmetic Operations are:      

Addition is:     40

Subtraction is:  0

The multiplication is:   400

Division is:     1.0

The remainder is:        0

Examples given above explained using classes and objects are simplest form OOP using python. But it will not work in real life problems/ applications.

Self Method():

Self method in python represent instance of class. By using self method we can access members of the class. Self always pointing current object.


The  __init__method():

The __init__ function is built in function in Python. The  _ _ init()_ _method is similar to constructor in c++ and Java. Every class have this built in function in python. This method is run when an objects are created. 

The use of __init__() function to assign the values to the properties of object and perform other operations.

The __init__(self) is a special method to initialize the variables of class. In the __init__ method two underscores before and after are used this means the method is internally defined and we cannot call it explicitly. self written in method name in the parenthesis refers to current class instance. It allocate a separate memory block on heap. 

To refer instance variables we can use dot operator notation along with self as,

self.name=name

self.age=age

Use of self and __init__ method

# class definition

class person:

    #__init__ function()

    def __init__(self, fname,mname,lname):

        self.fname=fname

        self.mname=mname

        self.lname=lname

obj=person("Manisha","M","More")

print(obj.fname,obj.mname,obj.lname)

O/P:

Manisha M More


__init__()function is called automatically every time when the class is being used to create a new object.

Self parameter is used as a reference to the current instance of the class. It is also used to access variables that belongs to class.  The self has to named by any name as you wish. It is first parameter of any function of class. 

A program to display factorial using class

# Define class 

class factorial:

# member function of class 

    def getfactorial(self, n):

        fact = 1

        for i in range(1, n + 1):

            fact = fact * i

            i=i+1

        return fact

 num = int(input("Enter a Number: "))

 obj = factorial()

print("\nFactorial of", num, "=", obj.getfactorial(num))

 O/P:

Enter a Number: 4

 Factorial of 4 = 24


 


टिप्पणी पोस्ट करा

0 टिप्पण्या