Hướng dẫn is __ init __ mandatory in python? - __ init __ là bắt buộc trong python?

Không, nó không cần thiết.

Ví dụ.

class A(object):
    def f():
        print 'foo'

Và tất nhiên bạn có thể sử dụng nó, theo cách này:

a = A()
a.f()

Trong thực tế, bạn thậm chí có thể xác định một lớp theo cách này.

class A:
    pass

Tuy nhiên, việc xác định __init__ là một thông lệ phổ biến bởi vì các trường hợp của một lớp thường lưu trữ một số loại thông tin hoặc dữ liệu trạng thái và các phương pháp của lớp cung cấp một cách để thao tác hoặc làm điều gì đó với thông tin hoặc dữ liệu trạng thái đó. __init__ cho phép chúng tôi khởi tạo thông tin hoặc dữ liệu trạng thái này trong khi tạo một thể hiện của lớp.

Đây là một ví dụ hoàn chỉnh.

class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()

Một thể hiện của lớp luôn được truyền như là đối số đầu tiên cho một phương thức của lớp. Ví dụ: nếu có

a = A()
a.f()
0 và bạn có một phiên bản
a = A()
a.f()
1, bất cứ khi nào bạn gọi
a = A()
a.f()
2,
a = A()
a.f()
3 sẽ tự động gọi
a = A()
a.f()
4 của
a = A()
a.f()
0. (Lưu ý đối số đầu tiên.) Theo quy ước, chúng tôi đặt tên cho đối số đầu tiên này là
a = A()
a.f()
6.

Điều kiện tiên quyết-Lớp Python, đối tượng, bản thân bất cứ khi nào lập trình hướng đối tượng được thực hiện trong Python, chúng tôi chủ yếu bắt gặp phương pháp __init__ trong OOPS mà chúng tôi thường không hiểu hoàn toàn. Bài viết này giải thích khái niệm chính của __init__ nhưng trước khi hiểu __init__ một số điều kiện tiên quyết được yêu cầu.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.

__Init__ trong Python là gì?

Chất xây dựng __init__ mặc định trong C ++ và Java. Các hàm tạo được sử dụng để khởi tạo trạng thái đối tượng. Nhiệm vụ của các hàm tạo là khởi tạo (gán giá trị) cho các thành viên dữ liệu của lớp khi một đối tượng của lớp được tạo. Giống như các phương thức, một hàm tạo cũng chứa một tập hợp các câu lệnh (nghĩa là hướng dẫn) được thực thi tại thời điểm tạo đối tượng. Nó được chạy ngay khi một đối tượng của một lớp được khởi tạo. Phương pháp này rất hữu ích để thực hiện bất kỳ khởi tạo nào bạn muốn làm với đối tượng của mình.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 at 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

a = A()
a.f()
7
a = A()
a.f()
8

a = A()
a.f()
9
class A:
    pass
0
class A:
    pass
1
a = A()
a.f()
6
class A:
    pass
3

class A:
    pass
4
a = A()
a.f()
6
class A:
    pass
6
class A:
    pass
7
class A:
    pass
8

a = A()
a.f()
9
class A:
    pass
0
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
1
a = A()
a.f()
6
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
3

class A:
    pass
4
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
5
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
6
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
7
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
8
a = A()
a.f()
6
Hello, my name is Nikhil
0

Hello, my name is Nikhil
1
class A:
    pass
7
Hello, my name is Nikhil
3
Hello, my name is Nikhil
4
Hello, my name is Nikhil
5

Hello, my name is Nikhil
6

Output:

Hello, my name is Nikhil

Hiểu mã

Trong ví dụ trên, một tên người Nikhil được tạo ra. Trong khi tạo ra một người, thì Nik Nikhil được thông qua như một đối số, đối số này sẽ được chuyển sang phương thức __init__ để khởi tạo đối tượng. Từ khóa tự đại diện cho thể hiện của một lớp và liên kết các thuộc tính với các đối số đã cho. Tương tự, nhiều đối tượng của lớp người có thể được tạo bằng cách truyền các tên khác nhau làm đối số. Dưới đây là ví dụ về init trong python với các tham sốinit in python with parameters

Ví dụ về __init __ & nbsp;

Python3

a = A()
a.f()
7
a = A()
a.f()
8

a = A()
a.f()
9
class A:
    pass
0
class A:
    pass
1
a = A()
a.f()
6
class A:
    pass
3

class A:
    pass
4
a = A()
a.f()
6
class A:
    pass
6
class A:
    pass
7
class A:
    pass
8

a = A()
a.f()
9
class A:
    pass
0
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
1
a = A()
a.f()
6
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
3

class A:
    pass
4
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
5
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
6
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
7
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
8
a = A()
a.f()
6
Hello, my name is Nikhil
0

class A:
    pass
4
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
5
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
6
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
7
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
8
a = A()
a.f()
6
Hello, my name is Nikhil
0

Hello, my name is Nikhil
1
class A:
    pass
7
Hello, my name is Nikhil
3
Hello, my name is Nikhil
4
Hello, my name is Nikhil
5

Hiểu mã

__init__6

__init__7

__init__8

Output:

Hello, my name is Nikhil
Hello, my name is Abhinav
Hello, my name is Anshul

Trong ví dụ trên, một tên người Nikhil được tạo ra. Trong khi tạo ra một người, thì Nik Nikhil được thông qua như một đối số, đối số này sẽ được chuyển sang phương thức __init__ để khởi tạo đối tượng. Từ khóa tự đại diện cho thể hiện của một lớp và liên kết các thuộc tính với các đối số đã cho. Tương tự, nhiều đối tượng của lớp người có thể được tạo bằng cách truyền các tên khác nhau làm đối số. Dưới đây là ví dụ về init trong python với các tham số

Ví dụ về __init __ & nbsp;

Python3

B init called
A init called
1
class A:
    pass
7
Hello, my name is Nikhil
3
Hello, my name is Nikhil
4
Hello, my name is Nikhil
5

a = A()
a.f()
9
class A:
    pass
0
class A:
    pass
1
a = A()
a.f()
6__init__7

class A:
    pass
4
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
5
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
6
a = A()
a.f()
01
Hello, my name is Nikhil
5

class A:
    pass
4
a = A()
a.f()
6
a = A()
a.f()
05
class A:
    pass
7
a = A()
a.f()
07

a = A()
a.f()
7
a = A()
a.f()
09

a = A()
a.f()
9
class A:
    pass
0
class A:
    pass
1
a = A()
a.f()
6__init__7

class A:
    pass
4
a = A()
a.f()
16
a = A()
a.f()
6
a = A()
a.f()
18

class A:
    pass
4
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
5
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
6
a = A()
a.f()
22
Hello, my name is Nikhil
5

class A:
    pass
4
a = A()
a.f()
6
a = A()
a.f()
05
class A:
    pass
7
a = A()
a.f()
07

a = A()
a.f()
29
class A:
    pass
7
a = A()
a.f()
31
a = A()
a.f()
32
Hello, my name is Nikhil
5

Output:

A init called
B init called

Lưu ý: Để biết thêm về kế thừa bấm vào đây.

Example:  

Python3

a = A()
a.f()
7 __init__0__init__1
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
3

a = A()
a.f()
9
class A:
    pass
0
class A:
    pass
1
a = A()
a.f()
6__init__7

class A:
    pass
4
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
5
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
6
a = A()
a.f()
01
Hello, my name is Nikhil
5

class A:
    pass
4
a = A()
a.f()
6
a = A()
a.f()
05
class A:
    pass
7
a = A()
a.f()
07

a = A()
a.f()
7
a = A()
a.f()
09

a = A()
a.f()
9
class A:
    pass
0
class A:
    pass
1
a = A()
a.f()
6__init__7

class A:
    pass
4
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
5
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()
6
a = A()
a.f()
22
Hello, my name is Nikhil
5

class A:
    pass
4
a = A()
a.f()
6
a = A()
a.f()
05
class A:
    pass
7
a = A()
a.f()
07

class A:
    pass
4
a = A()
a.f()
16
a = A()
a.f()
6
a = A()
a.f()
18

a = A()
a.f()
7
a = A()
a.f()
09

Output:

B init called
A init called

a = A()
a.f()
29
class A:
    pass
7
a = A()
a.f()
31
a = A()
a.f()
32
Hello, my name is Nikhil
5
To know more about inheritance click here.


__ init __ bắt buộc?

Nó không bắt buộc.Nhưng nếu bạn muốn phương pháp A. __init__ để xử lý khởi tạo của chính nó và B. __init __ () chỉ là một cái gì đó nhiều hơn, bạn sẽ gọi nó.. But if you want the A. __init__ method to handle its own initialization and B. __init__() just something bit more, you wave to call it.

Tại sao __ init __ quan trọng?

__init__ là một trong những phương pháp dành riêng trong Python.Trong lập trình định hướng đối tượng, nó được gọi là một hàm tạo.Phương thức __init__ có thể được gọi khi một đối tượng được tạo từ lớp và cần truy cập để khởi tạo các thuộc tính của lớp.access is required to initialize the attributes of the class.

Mỗi lớp có yêu cầu init không?

Mỗi lớp nên có một phương thức với tên đặc biệt __init__.Phương thức khởi tạo này được gọi tự động bất cứ khi nào một phiên bản mới của điểm được tạo.Nó cung cấp cho lập trình viên cơ hội để thiết lập các thuộc tính cần thiết trong phiên bản mới bằng cách cung cấp cho họ trạng thái/giá trị ban đầu của chúng.. This initializer method is automatically called whenever a new instance of Point is created. It gives the programmer the opportunity to set up the attributes required within the new instance by giving them their initial state/values.

__ init __ được gọi là tự động?

Lưu ý: Hàm __init __ () được gọi tự động mỗi khi lớp được sử dụng để tạo một đối tượng mới.The __init__() function is called automatically every time the class is being used to create a new object.