Python Object and Python Class

Python is an object oriented programming language as it focuses on the objects. A class can be defined as a prototype or blueprint used to create....

In this article we will going to learn about Python objects and Python classes, how to define a python class, how to create a python object, python constructors, how to delete an attribute and object in python with the help of examples.

Python Object and Python Class

Python Class

Python is an object oriented programming language as it focuses on the objects. A class can be defined as a prototype or blueprint used to create objects. In simple terms we can say that a class is used as a constructor for making objects. Class provides us a way to the bundling of data and methods together. And we can create a new type of object by creating a new class and it will allows this type of new instance to be made. 

How to Define Python class

We can create a class in python by writing the class name after the class keyword. Following is the syntax to create a class in python;

Syntax

class MyClass:
    # class definition 

Python Object

An object is known as the instance of the class and the process of creating object is called instantiation. In simple terms, an object is a collection of data and methods which acts on the data. We can create as many objects as we want in a particular class.

How to Create Python object

A python class is said to be instantiated when we create an object of a class. In order to create an object or instance in python we have to call the class by using the class name. Following is the syntax to create an object or instance in python;

Syntax

objectName = ClassName()

Let's understand with following example,

# create class

class MyClass:

    name = ""

    marks = 0

# create objects of class

MyClass1 = MyClass()

Here, is the object of the class. Now, we can use this object to access the class attributes.

Note: By using the . notation we can access the attributes of a particular python class.

# modify the name attribute

MyClass1.name = "Deepak Thakur"

# access the marks attribute

MyClass1.marks

Here, we have used and  to change and access the value of name and marks attribute respectively.

Let's understand Python Class and Objects with example,

# define a Python class

class MyClass:

    name = ""

    marks = 0

# create Python object of class

MyClass1 = MyClass()

# access attributes and assign new values

MyClass1.marks = 97

MyClass1.name = "Deepak Thakur"

print(f"Name: {MyClass1.name}, Marks: {MyClass1.marks} ")

Output

Name: Deepak Thakur, Marks: 97 

How to Delete Python Object 

In python, by using the del keyword we can delete the object or properties of the object. With the help of following example let's understand how to delete an object in python;

class MyClass1:  

    name = "Deepak Thakur"  

    marks = 97  

    def display(self):  

        print("name: %d \nmarks: %s" % (self.name, self.marks))  

    # Creating a MyClass instance of MyClass1 class  

MyClass = MyClass1()  

# Deleting the property of object  

del MyClass.name 

# Deleting the object itself  

del MyClass

MyClass.display()  

Output

AttributeError: name

Python Object Methods

We can also create python functions in the object which are known as Methods.

The self parameter

The parameter is used as a reference for the current instance of a python class and it allows us to access the variables of that particular python class. In place of self you can use whatever you like but it should only be the first parameter of any functions of that particular class.

The __init__() function

In python, all classes have the function and this function is executed when we initialize the class or create object. This method is used to assign the values to object properties.

class MyClass1:

  def __init__(self, name, marks):

    self.name = name

    self.marks = marks

  def myfunc(self):

    print("Hey! My name is " + self.name)

MyClass = MyClass1("Deepak Thakur", 97)

MyClass.myfunc()

Output

Hey! My name is Deepak Thakur

Conclusion

Above we have discussed about about Python objects and Python classes, how to define a python class, how to create a python object, python constructors, how to delete an attribute and object in python with the help of examples. A class can be defined as a prototype or blueprint used to create objects. An object is known as the instance of the class and the process of creating object is called instantiation.