Python has been an object-oriented language since it existed. Because of this, creating and using classes and objects are downright easy
However, here is small introduction of Object-Oriented Programming OOP to bring you at speed.
Python is an object-oriented programming language. What this means is we can solve a problem in Python by creating objects in our programs. In this guide, we will discuss OOPs terms such as class, objects, methods etc. along with the Object oriented programming features such as inheritance, polymorphism, abstraction, encapsulation.
An object is an entity that has attributes and behaviour. For example, Ram is an object who has attributes such as height, weight, color etc. and has certain behaviours such as walking, talking, eating etc.
A class is a blueprint for the objects. For example, Ram, Shyam, Steve, Rick are all objects so we can define a template (blueprint) class Human for these objects. The class can define the common attributes and behaviours of all the objects.
As we discussed above, an object has attributes and behaviours. These behaviours are called methods in programming.
Example of Class and Objects:
In this example, we have two objects Ram and Steve that belong to the class Human
Object attributes: name, height, weight
Object behaviour: eating()
Define class in Python
A class is defined using the keyword class.
In this example, we are creating an empty class DemoClass. This class has no attributes and methods.
The string that we mention in the triple quotes is a docstring which is an optional string that briefly explains the purpose of the class.
Creating Objects of class
In this example, we have a class MyNewClass that has an attribute num and a function hello(). We are creating an object obj of the class and accessing the attribute value of object and calling the method hello() using the object.