Classes and Objects in Python with Examples

In this article, We are going to cover classes and objects in python with examples, The self Parameter in Python, __init__method in Python, Modify Object Properties in Python, Delete Object in Python.

What is Object-oriented programming (OOP):

Object-oriented programming is another name is real-world programming.

Object-oriented programming is teach us how to understand programming concept by comparing them with real-world objects. It is a concept that combines both data and the functions that operate on that data into a single unit called the object. An object can be define a data field that has unique attributes and behavior.

Property: Variable that can be used anywhere in class.

Methods: Functions in the collection.

For example student  

Properties: rollno, name, address, mobileno, email.

Methods: getinfo(), printinfo(), result(), placement().

Classes and Objects in Python:

What is Class in Python ?

A Class is like a “blueprint” for creating objects. Class holds its own data members and member functions, which can be accessed and used by creating instance of that class.

Classes are created by keyword class.

Syntax of class in python:

class classname:
	statement 1
	.
	.
	statement N 

Example of class in Python:

class Student:
  name = "Lily"
print(Student)

Output:

<class '__main__.Student'>
Classes and Objects in Python with Examples 1

What is Object in Python:

An object is an instance of class. Object is any real thing in the world which can see, touch, feel or being presentable to the sense, it have its own properties and methods on which the object is identified.

Properties are called as variables, and methods are called functions.

For example mobile:

State: ringing, switch off, vibrate.

Properties: height, width, camera, screen, touch, headphones, charger.

Method: dial a call, receive a call, taking picture, songs and movies.

Example of object in python:

class Student:
  name = "Lily"
stud = Student()
print(stud.name)

Output:

Lily
Classes and Objects in Python with Examples 2

The self Parameter in Python:

The self parameter is a reference to the current instance of the class, and I used to access variables that belongs to the class.

To self, you can call it whatever you like, but it has to be the first parameter of any function on the class.

Example of self parameter in Python:

class Student:  
    name = "Lily"
    age =  22
    def myfun(self):  
        print("I'm a", self.name) 
        print("I'm", self.age, "years old") 
  
stud = Student() 
stud.myfun()

Output:

I'm a Lily
I'm 22 years old
Classes and Objects in Python with Examples 3

__init__method in Python

Class function that begin with double underscore (__) are called magic function dunder.

All classes have a function called __init__() which is always executed when the class is being initiated.

The method is useful to do any initialization you want to do with object.

Example of __init__ method in Python:

class Student: 
	def __init__(self, name, city): 
		self.name = name
		self.city = city 
	def myfun(self): 
		print('Hello my name is', self.name)
		print('I am from', self.city )

stud = Student('Lily', 'Mukhed') 
stud.myfun()

Output:

Hello my name is Lily
I am from Mukhed
Classes and Objects in Python with Examples 4

Modify Object Properties in Python:

You can modify properties on object.

Example of Modify Object Properties:

class Student: 
	def __init__(self, name, city): 
		self.name = name
		self.city = city 
	def myfun(self): 
		print('Hello my name is', self.name)
		print('I am from', self.city )

stud = Student('Lily', 'Latur')
# city modify
stud.city = 'Nashik'
stud.myfun()

Output:

Hello my name is Lily
I am from Nashik
Classes and Objects in Python with Examples 5

Delete Object in Python

We can delete objects by using the del keyword.

Example of delete object in python:

class Student: 
	def __init__(self, name, city): 
		self.name = name
		self.city = city 
	def myfun(self): 
		print('Hello my name is', self.name)
		print('I am from', self.city )
stud = Student('Lily', 'Latur')
del stud
stud.myfun()

Output:

Traceback (most recent call last):
  File "C:/Users/HP/Desktop/python/init_fun.py", line 10, in <module>
    stud.myfun()
NameError: name 'stud' is not defined

# stud object deleted
Classes and Objects in Python with Examples 6

Conclusion:

We have covered classed and objects in python, The self Parameter in Python, __init__method in Python, Modify Object Properties in Python, Delete Object in Python.

Related Articles:

Functions in Python with Examples

Loops in Python 3 with Examples

7 Python Operators with Examples

Python Introduction for Programmers [Part 1]

6 Python Conditional Statements with Examples

Inheritance in Python with Examples

Reference:

Python official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap