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[]
8a = A[]
a.f[]
9class A:
pass
0 class A:
pass
1a = A[]
a.f[]
6class A:
pass
3class A:
pass
4a = A[]
a.f[]
6class A:
pass
6class A:
pass
7 class A:
pass
8a = A[]
a.f[]
9class 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[]
1a = A[]
a.f[]
6class 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[]
3class A:
pass
4class 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[]
5class 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[]
6class 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[]
7class 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[]
8a = A[]
a.f[]
6Hello, my name is Nikhil0
Hello, my name is Nikhil6
Output:
Hello, my name is Nikhil
Hello, my name is Nikhil
1class A:
pass
7 Hello, my name is Nikhil
3Hello, my name is Nikhil
4Hello, my name is Nikhil
5
class A:
pass
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[]
8a = A[]
a.f[]
9class A:
pass
0 class A:
pass
1a = A[]
a.f[]
6class A:
pass
3class A:
pass
4a = A[]
a.f[]
6class A:
pass
6class A:
pass
7 class A:
pass
8class A:
pass
4class 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[]
5class 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[]
6class 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[]
7class 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[]
8a = A[]
a.f[]
6Hello, my name is Nikhil0
a = A[]
a.f[]
9class 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[]
1a = A[]
a.f[]
6class 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[]
3class A:
pass
4class 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[]
5class 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[]
6class 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[]
7class 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[]
8a = A[]
a.f[]
6Hello, my name is Nikhil0
Hello, my name is Nikhil1
class A:
pass
7 Hello, my name is Nikhil3
Hello, my name is Nikhil4
Hello, my name is Nikhil5
__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__
1class 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[]
3a = A[]
a.f[]
9class A:
pass
0 class A:
pass
1a = A[]
a.f[]
6__init__
7class A:
pass
4class 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[]
5class 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[]
6a = A[]
a.f[]
01Hello, my name is Nikhil5
class A:
pass
4a = A[]
a.f[]
6a = A[]
a.f[]
05class A:
pass
7 a = A[]
a.f[]
07a = A[]
a.f[]
7 a = A[]
a.f[]
09a = A[]
a.f[]
9class A:
pass
0 class A:
pass
1a = A[]
a.f[]
6__init__
7class A:
pass
4a = A[]
a.f[]
16a = A[]
a.f[]
6a = A[]
a.f[]
18class A:
pass
4class 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[]
5class 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[]
6a = A[]
a.f[]
22Hello, my name is Nikhil5
class A:
pass
4a = A[]
a.f[]
6a = A[]
a.f[]
05class A:
pass
7 a = A[]
a.f[]
07a = A[]
a.f[]
7 a = A[]
a.f[]
09
Output:
A init called B init called
a = A[]
a.f[]
29class A:
pass
7 a = A[]
a.f[]
31a = A[]
a.f[]
32Hello, my name is Nikhil5
Example:
Python3
a = A[]
a.f[]
7 __init__
0__init__
1class 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[]
3a = A[]
a.f[]
9class A:
pass
0 class A:
pass
1a = A[]
a.f[]
6__init__
7class A:
pass
4class 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[]
5class 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[]
6a = A[]
a.f[]
01Hello, my name is Nikhil5
class A:
pass
4a = A[]
a.f[]
6a = A[]
a.f[]
05class A:
pass
7 a = A[]
a.f[]
07a = A[]
a.f[]
7 a = A[]
a.f[]
09a = A[]
a.f[]
9class A:
pass
0 class A:
pass
1a = A[]
a.f[]
6__init__
7class A:
pass
4class 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[]
5class 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[]
6a = A[]
a.f[]
22Hello, my name is Nikhil5
class A:
pass
4a = A[]
a.f[]
6a = A[]
a.f[]
05class A:
pass
7 a = A[]
a.f[]
07class A:
pass
4a = A[]
a.f[]
16a = A[]
a.f[]
6a = A[]
a.f[]
18a = A[]
a.f[]
7 a = A[]
a.f[]
09Output:
B init called A init called
29a = A[]
a.f[]
7 class A:
pass
31a = A[]
a.f[]
32a = A[]
a.f[]
Hello, my name is Nikhil
5To know more about inheritance click here.