How to instantiate a class in Python?
As in many programming languages, the Python classes start with the class
keyword and continue with the name of the class.
class MyClassName:
pass
The instance of the class is created by calling the name of the class. The instance is also referred to as an object.
class MyClassName:
pass
obj = MyClassName()
Class constructor
The initialization of the class invokes __init__()
method, which is used to handle input parameters. To illustrate, the Student
class takes name
and age
parameters by using __init__
as shown below code.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
first_student = Student("Emre", 19)
second_student = Student("Ayse", 20)
print(F"Student names: {first_student.name}, {second_student.name}")
The output of the above code:
Student names: Emre, Ayse
The first parameter named self
of the init method refers to the instance of the class. The keyword self
is a mandatory parameter if the __init__ method is used. Conversely, the following input parameters (first_variable
, second_variable
, third_variable
) are optional and may vary depending on the programmer’s design.
def __init__(self, first_variable, second_variable, third_variable):
Instance methods and variables
The instance methods of a class should take self
as the first parameter. Otherwise, an error will occur.
class Student:
def __init__(name):
pass
The error message due to above code.
TypeError: Student.__init__() takes 1 positional argument but 2 were given
If the value of the input parameter needs to be stored in the instance of the class, a variable name with self should be used. It is not mandatory to use the same name for the instance variable and input variable. To clarify, as shown below, the input parameter of the init method is name
, and the instance parameter is student_name
. If the instance variable is called (as in the print
method), the student_name
should be used. On the other hand, the name
is used to instantiate the Student
class, and constructing the instance name
will never be used.
class Student:
def __init__(self, name, age):
self.student_name = name
self.student_age = age
registered_student = Student(
name = "Ceren",
age = 25
)
print(registered_student.student_name)
print(registered_student.student_age)
Instantiation without a constructor
A class can be instantiated without using the __init__ method. In addition, instance variables self.name
and self.age
can be created inside any instance method as shown code below.
class Student:
def define_values(self, name, age):
self.name = name
self.age = age
student = Student()
student.define_values("John", 22)
print(student.name)
print(student.age)
On the contrary, if the variable is called before defining (as inside the print
method) an error will be prompted as shown code below.
class Student:
def define_student_name(self, name):
self.name = name
student = Student()
print(student.name)
student.define_student_name("Nazrin")
Error due to the above code.
AttributeError: 'Student' object has no attribute 'name'
Accessing instance members within instance methods
Instance variables or methods can be called inside another instance method. To illustrate, self.name
and self.surname
are called inside get_student_fullname
method.
class Student:
def __init__(self, name, surname):
self.name = name
self.surname = surname
def get_student_fullname(self):
return self.name + " " + self.surname
student = Student("Aygun", "Kazimova")
print(student.get_student_fullname())
Conclusion
This post contains a simple explanation of how to instantiate a class, and we can call or design classes in different ways.