Cải thiện bài viết
Lưu bài viết
Phương pháp Dunder hoặc ma thuật trong Python là các phương pháp có hai tiền tố và hậu tố nhấn mạnh trong tên phương thức. Dunder ở đây có nghĩa là Double Double Under (nhấn mạnh). Chúng thường được sử dụng để vận hành quá tải. Một vài ví dụ cho các phương pháp ma thuật là: __init__, __add__, __len__, __repr__, v.v. Trong bài viết này, chúng ta sẽ thấy sự khác biệt giữa hai phương pháp như vậy.
Lưu ý: Để biết thêm thông tin, hãy tham khảo Phương pháp Dunder hoặc Phép thuật trong Python For more information, refer to Dunder or magic methods in Python
__init__()
Phương pháp Python này tương tự như một hàm tạo trong bất kỳ ngôn ngữ lập trình nào khác. Một hàm tạo là một định nghĩa có cùng tên với lớp và được gọi tự động khi đối tượng của lớp đó được xác định. Một hàm tạo khởi tạo tất cả các thực thể cần thiết của chương trình để làm cho nó đáng tin cậy hơn. Nhiễu với định nghĩa này __init __ () hoạt động như một hàm tạo trăn, nó được gọi tự động khi một đối tượng của lớp được xác định. Nó khởi tạo các thành viên cần thiết với các giá trị mặc định được cung cấp. Nó cũng có thể được gọi với các giá trị được cung cấp trong thời gian khai báo đối tượng của lớp.
Similar
to this definition __init__() works as a Python constructor it is invoked automatically when an object of the class is defined. It initializes the required members with default values provided. It can also be invoked with the values provided during the time of declaration of the object of the class.
EXAMPLE:
class A:
def __init__(self
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 123inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120selfinside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 127inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 129
def __init__, __add__, __len__, __repr__22.
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122__init__, __add__, __len__, __repr__8inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122class3class4class5inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122selfclass8
class9inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 A:1A:2inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
A:4
A:5inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 A:1A:8inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
0
OUTPUT:
inside __init__() inside __str__() ('value of y:', '3') inside __init__() inside __str__() ('value of y:', '10')__call__()
Trước khi áp dụng 1, chúng ta cần hiểu đối tượng có thể gọi được là gì. Đối tượng có thể gọi là một đối tượng có thể được gọi giống như một hàm. Trong Python, __call __ () được sử dụng để giải quyết mã liên quan đến đối tượng có thể gọi được. Bất kỳ đối tượng nào cũng có thể được chuyển đổi thành một đối tượng có thể gọi chỉ bằng cách viết nó theo định dạng gọi hàm. Một đối tượng của loại đó gọi phương thức __call __ () và thực thi mã được liên kết với nó. Điều này không làm cho đối tượng không hoạt động như một người bình thường. Đối tượng có thể được sử dụng làm bình thường được sử dụng. Một điều cần lưu ý là đối tượng được sử dụng làm hàm, vì vậy cú pháp sẽ đúng.
A callable object is one
which can be called like a function.
In Python, __call__() is used to resolve the code associated with a callable object. Any object can be converted to a callable object just by writing it in a function call format. An object of that kind invokes the __call__() method and executes the code associated with it. This doesn’t make the object not to work like a normal one. The object can be used as a normal is used.
One thing to keep in mind is the object is itself used as a function, so
syntax should be right.
EXAMPLE:
class A:
def __init__(self
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 123inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120selfinside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 127inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 129
def __init__, __add__, __len__, __repr__22.
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122__init__, __add__, __len__, __repr__8inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122class3class4class5inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122selfclass8
class9inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 A:1A:2inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
A:5inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 A:1A:8inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1200inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 122inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1205inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
Trước khi áp dụng 1, chúng ta cần hiểu đối tượng có thể gọi được là gì. Đối tượng có thể gọi là một đối tượng có thể được gọi giống như một hàm. Trong Python, __call __ () được sử dụng để giải quyết mã liên quan đến đối tượng có thể gọi được. Bất kỳ đối tượng nào cũng có thể được chuyển đổi thành một đối tượng có thể gọi chỉ bằng cách viết nó theo định dạng gọi hàm. Một đối tượng của loại đó gọi phương thức __call __ () và thực thi mã được liên kết với nó. Điều này không làm cho đối tượng không hoạt động như một người bình thường. Đối tượng có thể được sử dụng làm bình thường được sử dụng. Một điều cần lưu ý là đối tượng được sử dụng làm hàm, vì vậy cú pháp sẽ đúng.
def , x):0self__init__, __add__, __len__, __repr__4
class9inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 A:1A:2inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
A:4
A:5inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 A:1A:8inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1227
A:5inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 A:1A:8inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 124
0
Trước khi áp dụng 1, chúng ta cần hiểu đối tượng có thể gọi được là gì. Đối tượng có thể gọi là một đối tượng có thể được gọi giống như một hàm. Trong Python, __call __ () được sử dụng để giải quyết mã liên quan đến đối tượng có thể gọi được. Bất kỳ đối tượng nào cũng có thể được chuyển đổi thành một đối tượng có thể gọi chỉ bằng cách viết nó theo định dạng gọi hàm. Một đối tượng của loại đó gọi phương thức __call __ () và thực thi mã được liên kết với nó. Điều này không làm cho đối tượng không hoạt động như một người bình thường. Đối tượng có thể được sử dụng làm bình thường được sử dụng. Một điều cần lưu ý là đối tượng được sử dụng làm hàm, vì vậy cú pháp sẽ đúng.
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 121inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1227
OUTPUT:
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 12def , x):0self__init__, __add__, __len__, __repr__4
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120, x):4inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 , x):6 | ____10, x):4inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 selfinside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 127inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1212 inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1213 |
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 120inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1215 inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1216 | inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1223inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1225 |
inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1223inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 128 inside __init__() inside __str__() ('value of y:', '3') inside __call__() adding 2 to the value of y 5 inside __init__() inside __str__() ('value of y:', '10') inside __call__() adding 2 to the value of y 1236 | Sự khác biệt giữa __init __ () vs __call __ () |
Giống như hàm tạo, nó khởi tạo các giá trị a=A(3) #statement 1 a() #statement 2 __init__() is called by statement 1 | Nó được sử dụng cho một cuộc gọi trực tiếp bằng cách sử dụng đối tượng a=A(3) #statement 1 a() #statement 2 __call__() is called by statement 2 |