Classes and Objects in Python

Classes and Objects in Python

Classes and Objects in Python

Classes and Objects in Python

Class

In object oriented programming, a class contains procedures and data members. And treated as an abstract data type.

Object

An object is an instance of a class.

 

In this post, I will discuss about classes and objects in Python programming.

Let us start with creating a simple Python’s class.

Class in Python is preceded by class keyword and is followed by a colon(:).

See an example of creating an empty class in Python.

Empty Class in Python

class Rectangle:
pass
pass statement is necessary to create an empty class.

Class with Member Variables

A class which is empty which can not be used in any real life applications. You can declare data members inside a class to use it further.
In rectangle class I have declared two variables “length” and “width” and assigned values 5 and 6 respectively. Once data members are initialized in class Rectangle they can be accessed using objects of class Rectangle.
class Rectangle:
length=5
width=6
object1=Rectangle() #Creating an object object1 of class Rectangle
print(‘Length of Rectangle is’,object1.length)
print(‘Width of Rectangle is’, object1.width)

Output:
Length of Rectangle is 5
Width of Rectangle is 6

In the above class, object1 is created of class Rectangle and using the object object1 data members length and width are accessed.

Class with Member Variables and a Mathematical Operation

A mathematical operation can also be performed inside a class and can be printed.
See an example

class Rectangle:
length=5
width=6
area=length * width
print(‘Area of Rectangle is’,area )
object2=Rectangle()

Output:
Area of Rectangle is 30

Class with a Function

A function inside a class is called a method. A method area() is defined within Rectangle class. You can notice an argument self within area method which is used to initiate variables of a function which I will discuss in the next section. In the below method I have only used length
and width variables which are defined within method area().

class Rectangle:
def area(self):
length=5
width=7
print(‘Area of Rectangle is’, length * width)
object3= Rectangle()

Output:
Area of Rectangle is 35

Class with a Function with Arguments

In the below program, there are three arguments inside area() function self, length and width. Where the first argument self(you can use any name in place of self) is used to initialize variables of a method. Further, values of length and width are passed from called function and then self variable initializes both variables. Finally, inside print function area is calculated.
class Rectangle:
def area(self, length, width):
self.length=length
self.width=width
print(‘Area of Rectangle is’, self.length * self.width)
object4= Rectangle()
object5= Rectangle()
object4.area(5,8)
object5.area(5,9)
Output:
Area of Rectangle is 40
Area of Rectangle is 45

Class with a Function with Arguments and a Return Value

The the area() method of the above program is modified which returns a value which is area instead of printing inside the area() method.
class Rectangle:
def area(self, length, width):
self.length=length
self.width=width
return self.length * self.width
object4= Rectangle()
object5= Rectangle()
area1=object4.area(5,8)
area2=object5.area(5,9)
print(‘Area of Rectangle is’,area1)
print(‘Area of Rectangle is’area2)
Output:
Area of Rectangle is 40
Area of Rectangle is 45

Constructor in Python

Constructor is a special and it is called when an object is created. Python provides __init__() method to initialized objects and it is called when an object is created.
class Rectangle:
def __init__(self, length, width):
self.length=length
self.width=width
print(‘,Area of Rectangle is’self.length * self.width)
object6= Rectangle(5,10) # __init__() method is called when object6 is created with two argument.

Output:
Area of Rectangle is 50

Share to Your Friend
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Comment