Class in Python
Python is an object oriented programming language where everything in Python is considered as an object that includes properties and methods. A properties contain values for object and methods perform operations on the values. A class specifies the blueprint of an objects.
A class is created in Python by using keyword class followed by name of the class. A class definition includes properties and methods. A properties of the class are define using __init__() and methods are created by using def keyword.
Syntax
 1 class <class_name>:
 2   <properties> # define using __init__() method
 3   <methods> # methods using def keyword
class_name : A name of the class to define new type as class
properties : A properties of the class is define inside __init__() method
methods : A methods of the class is define for the class using def keyword
Class definition
 1 # definition of class
 2 class Employee:
 3   # properties of class
 4   def __init__(self, name, salary):
 5     self.name = name
 6     self.salary = salary
 7     
 8   # method of class
 9   def print(self):
 10     return f"name: {self.name}, salary: {self.salary}"
 11    
 12 # creating object of class
 13 emp = Employee("James", 10000)
 14 
 15 # access properties of object
 16 print("Employee name :", emp.name)
 17 print("Employee salary :", emp.salary)
 18 
 19 # calling method of class
 20 print(emp.print())
In the above example, a class Employee is define with properties and method. An object of class is created by passing arguments in the class constructor. A properties of the object is access and printed on console. A method of the class is called by using object of the class.
Output
 1 Employee name : James
 2 Employee salary : 10000
 3 name: James, salary: 10000

Class __init__() method

The class provide function __init__() to initialize the class object by providing the initial value as parameters. An init() function is always executed when the class is being initiated. It assign values to object properties or it allows to perform operation which required before creating instance of the class.

Class __str__() method

The __str__() function used to convert string representation of the object. It should returns a string representation of the object.

Custom class method

A class allows to define a custom method to serve the custom purpose of the class. A custom method can be define by using def keyword. A custom method get a reference of the instance object for which a method is called for. A properties of the object can be access using reference object to perform operation.

Object reference (self) parameter

The class self parameter refer the current class object to access the class object properties. It is similar to this keyword used in other programming language. It is not mandatory to have name of the parameter to object reference(self), it can have any other name.

Change object property

The class object property allows to change values after creating instance of class. An object property value can be change either by using dot operator and assigning new value to the property.

Remove object property and object

A class object and property of the class object can be deleted by using del operator. A del operator takes object as operand that needs to be deleted. It can be object itself or a property of the object.

Empty class

The class does not allows to define with empty definition. If a class can be define to validate the type, it can be define with empty definition. To define class without definition a pass keyword can be used.

Constructor

The object of class is created by using constructor by using the name of the class. A name of the class followed by parenthesis to create an object of class. A __init__() method will of the class will be called internally to initialize the properties of an object.

Inheritance

The inheritance is a process of deriving a class from the other class where derived class known as child class and the other class known as parent class. An inherit class inherits all the methods and properties from parent class.
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us