Topic: Python Class and Objects With Code Examples
How to create Class and Objects in Python
What is a class in Python?
A class is the specification of a set of objects of the same type. A class is a template or design plan to create objects of the same type.
A class is a template for objects. It contains the code for all the object’s methods.
Normally we will use a special method with name __init__() called constructor. The constructor will intialize the instance variables of the object automatically at the time of object creation.
Example of a class
Suppose we are to create a class to provide the functionality to add two numbers. It conatains:
- Two instance variables, num1, num2
- One Constructor
- One method add() to return the sum of two numbers.
Now we name this class as “Addition”. The first letter of each word is capital for a class name.
We will use two instance variables num1, num2.
We define a parameterized constructor to initialize the num1, num2 by the given values.
Note: The first argument in an instance method is a special variable called ‘self’. ‘self’ represents the current object upon which this method is called.
A method add() will return the addition of num1 and num2.
How to Define a Class in Python?
We define a class in Python using “class” keyword. We will define, in class:
- instance variable
- constructor
- methods
Example of a Class
# define a class Addition class Addition: # define a constructor def __init__(self, num1, num2): self.num1 = num1 self.num2 = num2 # define an instance method to add def add(self): return self.num1+ self.num2 # end of the class
What is a Constructor in Python?
A constructor is a special method with the name __init__ . The underscores indicate that it is a special kind of method. It is called a constructor, and it is automatically called when we create
a new object from a class. The constructor is usually used to initialize the instance variables of the object being created. This constructor is automatically called whenever a new object of this class is created.
How to define a constructor in Python?
Syntax: def __init__(self, arg1, arg2,...): self.var1 = arg1 self.var2 = arg2 ... Example: def __init__(self, num1, num2): self.num1 = num1 self.num2 = num2
What is an object in Python?
An object is an instance of the class. An object of the class will contain its seperate instance variables. The instance methods of the class will act upon the instance variables to process data of the object.
How to Create an Object in Python?
The basic syntax to create an object is:
object_name = Class(arg1,arg2,…)
Example Of Creating an Object in Python
Let us create an object aobject1 of Addition class:
object1 = Addition(10,20)
When above statement is executed, a new object of Addition class is created. This will call the constructor automatically with arguments 10 and 20. Hence a new object is created with values of num1=10 and num2=20. The object1 varible will have the reference to the new object.
How to Create an Instance Method in Class?
An instance method is a method defined in a class in Python to perform some action on object data – instance variables.
Syntax: def method_name(self): statement1 statement2 ... Example: def add(self): return self.num1 + self.num2 Note: self is a special variable representingthe current object. Always use self.instancc_variable when accessing instance variables.
How To Call an Instance Method
We will use dot notation to call an instance method.
object.method( )
And if this method returns some value then
variable = object.method( )
Example:
sum = object1.add( )
Complete Example Program Using class and Objects
# Python program to define a class Addition # with two instance variables num1, num2 # one constructor # one method add() # define class named Addition class Addition: # define constructor def __init__(self, num1, num2): self.num1 = num1 self.num2 = num2 # define add() instance method def add(self): return self.num1 + self.num2 # create an instance that is object of class Addition obj1 = Addition(80, 20) # call add method with obj1 object sum = obj1.add() print("Sum =", sum)
Output: Sum = 100