Mẹo Which function is called automatically every time the class is being used to create a new object?
Thủ Thuật Hướng dẫn Which function is called automatically every time the class is being used to create a new object? 2022
Bùi Nhật Dương đang tìm kiếm từ khóa Which function is called automatically every time the class is being used to create a new object? được Update vào lúc : 2022-10-06 01:08:05 . Với phương châm chia sẻ Bí kíp về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi Read Post vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.Prerequisites – Python Class, Objects, Self Whenever object-oriented programming is done in Python, we mostly come across __init__ method in oops which we usually don’t fully understand. This article explains the main concept of __init__ but before understanding the __init__ some prerequisites are required.
Nội dung chính- What is __init__ in Python?Understanding the codeExample of __init__ __init__ with inheritanceWhich of the following function is called when a class is used to create a new object in Python?Which function is automatically called when an object of a class is initiated?Which method is automatically created when an object is created?Is automatically called when a new object is initiated?
What is __init__ in Python?
The Default __init__ Constructor in C++ and Java. Constructors are used to initializing the object’s state. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed the time of Object creation. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.
Example:
Python3
class Person:
def __init__(self, name):
self.name = name
def say_hi(self):
print('Hello, my name is', self.name)
p = Person('Nikhil')
p.say_hi()
Output:
Hello, my name is NikhilUnderstanding the code
In the above example, a person name Nikhil is created. While creating a person, “Nikhil” is passed as an argument, this argument will be passed to the __init__ method to initialize the object. The keyword self represents the instance of a class and binds the attributes with the given arguments. Similarly, many objects of the Person class can be created by passing different names as arguments. Below is the example of init in python with parameters
Example of __init__
Python3
class Person:
def __init__(self, name):
self.name = name
def say_hi(self):
print('Hello, my name is', self.name)
p1 = Person('Nikhil')
p2 = Person('Abhinav')
p3 = Person('Anshul')
p1.say_hi()
p2.say_hi()
p3.say_hi()
Output:
Hello, my name is Nikhil Hello, my name is Abhinav Hello, my name is Anshul__init__ with inheritance
Inheritance is the capability of one class to derive or inherit the properties from some other class. Let’s consider the below example to see how __init__ works in inheritance.
Python3
class A(object):
def __init__(self, something):
print("A init called")
self.something = something
class B(A):
def __init__(self, something):
A.__init__(self, something)
print("B init called")
self.something = something
obj = B("Something")
Output:
A init called B init calledSo, the parent class constructor is called first. But in Python, it is not compulsory that the parent class constructor will always be called first. The order in which the __init__ method is called for a parent or a child class can be modified. This can simply be done by calling the parent class constructor after the body toàn thân of the child class constructor.
Example:
Python3
class A(object):
def __init__(self, something):
print("A init called")
self.something = something
class B(A):
def __init__(self, something):
print("B init called")
self.something = something
A.__init__(self, something)
obj = B("Something")
Output:
B init called A init calledNote: To know more about inheritance click here.
At each time you create an object, the __init__ method is called.
At each time you print an object, the __str__ method is called to get.
class Point: def __init__(self, x = 0, y = 0): print('__init__') self.x = x self.y = y def __str__(self): print('__str__') return "(0,1)".format(self.x,self.y) >>> p = Point(3, 2) __init__ >>> print(p) __str__ (3,2) >>> p <__main__.Point 0x7fa3ab341940>For the last case, the __str__ method is not called but __repr__. A good practice is to override this method to create a string representation of the creation of the instance like Point(3, 2):
def __repr__(self): print('__repr__') return f"Point(self.x, self.y)" >>> p = Point(4, 6) __init__ >>> p __repr__ Point(4, 6)
Post a Comment