Hướng dẫn is __ init __ compulsory in python? - __ init __ có bắt buộc trong python không?

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.

Tự là một quy ước và không phải là một từ khóa Python. Tự là tham số trong phương thức ví dụ và người dùng có thể sử dụng một tên tham số khác thay cho nó. Nhưng nên sử dụng bản thân vì nó làm tăng khả năng đọc của mã, và nó cũng là một thực hành lập trình tốt.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.

Đ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.

__Init__ trong Python là gì?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

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.

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
6

Output:

Hello, my name is Nikhil

Hello, my name is Nikhil1class A: pass 7 Hello, my name is Nikhil3Hello, my name is Nikhil4Hello, my name is Nikhil5

Hiểu mãinit in python with parameters

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ố

Python3

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.

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

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

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

__init__6

__init__7

__init__8

Output:

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

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ố

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
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()
7
a = A()
a.f()
09

Output:

A init called
B 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

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.


Có cần thiết phải xác định init trong Python khô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.

Tôi có thể viết một lớp Python mà không có init?

Chúng ta có thể tạo một lớp mà không có bất kỳ định nghĩa cấu trúc nào.Trong trường hợp này, hàm tạo siêu lớp được gọi để khởi tạo thể hiện của lớp.Lớp đối tượng là cơ sở của tất cả các lớp trong Python.. In this case, the superclass constructor is called to initialize the instance of the class. The object class is the base of all the classes in Python.

Các lớp học luôn cần init?

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.. This initializer method is automatically called whenever a new instance of Point is created.

Có bắt buộc phải sử dụng bản thân trong Python không?

Tự là một quy ước và không phải là một từ khóa Python.Tự là tham số trong phương thức ví dụ và người dùng có thể sử dụng một tên tham số khác thay cho nó.Nhưng nên sử dụng bản thân vì nó làm tăng khả năng đọc của mã, và nó cũng là một thực hành lập trình tốt.it is advisable to use self because it increases the readability of code, and it is also a good programming practice.