What is class and object in Python with example?

Python Classes and Objects

A python is an object-oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor or a “blueprint” for creating objects.

Create a class in Python

In python to create a class, we have used a keyword called class:

Syntax:

class Class_Name:
statement

Example:

Now we will create a class named called MyClass, with a property named value.

class MyClass:
value = 10

Create Object in Python

Now we will use a class name as MyClass to create an object.

Syntax:

class ClassName:
  variable = value

obj = ClassName()

Example:

class MyClass:
value = 10

obj = MyClass()
print(obj.value)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
10

Process finished with exit code 0

The __init__() Function:

The example which is given above is class and object in the simplest way and is not really useful in real-life applications.

To know the meaning of classes we have to know the built-in __init__() function.

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

Use the __init__() function to allocate values to object properties or other operations that are necessary to do when the object is being built:

Example:

class Student:
def __init__(self, s_name, s_age):
self.name = s_name
self.age = s_age
s1 = Student('Amresh', 22)
print(s1.name)
print(s1.age)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Amresh
22

Process finished with exit code 0

Object Methods:

An object is simply a collection of data and methods that act on those data.

Let’s create a method in the Student Class:

Example:

class Student:
def __init__(self, s_name, s_age):
self.name = s_name
self.age = s_age
def myfunc(self):
print('Hellow, My name is'+self.name)
s1 = Student('Amresh', 22)
s1.myfunc()

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Hellow, My name isAmresh

Process finished with exit code 0

How is self used in Python?

The self parameter is used to reference the current instance of the class, use to exceed the value that belongs to the class. You do not have to name yourself, you can call whatever you like, but it has to be a first parameter on the function in the class.

Example:

class Student:
def __init__(my_self, s_name, s_age):
my_self.name = s_name
my_self.age = s_age
def myfunc(cse):
print('Hellow, My name is'+cse.name)
s1 = Student('Amresh', 22)
s1.myfunc()

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Hellow, My name isAmresh

Process finished with exit code 0

How to modify Object Properties?

You can modify properties on objects in this way: s1.age = 24

Example:

class Student:
def __init__(self, s_name, s_age):
self.name = s_name
self.age = s_age
def myfunc(self):
print('Hellow, My name is'+self.name)
s1 = Student('Amresh', 22)
s1.age = 24
print(s1.age)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
24

Process finished with exit code 0

How to Delete Object Properties?

You can delete the properties from an object by using the del keyword:

Example:

class Student:
def __init__(self, s_name, s_age):
self.name = s_name
self.age = s_age
def myfunc(self):
print('Hellow, My name is'+self.name)
s1 = Student('Amresh', 22)
del s1.age
print(s1.age)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
File "C:/Users/Pramod/PycharmProjects/pythonProject/main.py", line 9, in <module>
print(s1.age)
AttributeError: 'Student' object has no attribute 'age'

Process finished with exit code 1

Pass Statement:

pass statement is used to avoid getting an error. When we define class then It should not be empty but if due to any reason class has defined with no content, at that time you can use a pass statement.

Example:

class Student:
pass

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment