Hướng dẫn how does __ call __ work in python? - __ gọi __ hoạt động như thế nào trong python?

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
12
0
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
12
1
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
12
2
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
12
3
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
12
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
12
0self
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
12
7
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
12
8
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
12
9

    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
12
0
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
12
1
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
12
2__init__, __add__, __len__, __repr__8
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
12
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
12
0
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
12
1
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
12
2class3class4class5
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
12
2selfclass8

class9

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
12
8 A:1A:2
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
12
4

A:4

A:5

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
12
8 A:1A:8
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
12
4

    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
12
0
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
12
1
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
12
2
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
12
3
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
12
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
12
0self
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
12
7
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
12
8
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
12
9

    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
12
0
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
12
1
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
12
2__init__, __add__, __len__, __repr__8
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
12
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
12
0
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
12
1
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
12
2class3class4class5
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
12
2selfclass8

class9

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
12
8 A:1A:2
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
12
4

A:5

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
12
8 A:1A:8
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
12
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
12
0
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
12
1
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
12
2
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
12
00
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
12
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
12
0
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
12
1
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
12
2
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
12
05
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
12
4

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

class9

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
12
8 A:1A:2
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
12
4

A:4

A:5

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
12
8 A:1A:8
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
12
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
12
1
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
12
27

A:5

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
12
8 A:1A:8
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
12
4

    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
12
1
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
12
27

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
12

    def , x):0self__init__, __add__, __len__, __repr__4

__init__()__call__()
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
12
0, x):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
12
8 , x):6
____10, x):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
12
8 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
12
7
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
12
12
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
12
13
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
12
0
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
12
15
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
12
16
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
12
23
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
12
8
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
12
25
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
12
23
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
12
8
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
12
36
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

Làm thế nào để __ chứa __ hoạt động trong Python?

Chuỗi Python __Contains __ () là một phương thức thể hiện và trả về giá trị boolean true hay false tùy thuộc vào việc đối tượng chuỗi có chứa đối tượng chuỗi được chỉ định hay không. Lưu ý rằng chuỗi Python chứa phương thức () là trường hợp nhạy cảm.returns boolean value True or False depending on whether the string object contains the specified string object or not. Note that the Python string contains() method is case sensitive.

Bạn có thể gọi trực tiếp phương thức __ str __ không?

Bạn có thể gọi trực tiếp phương thức __ str __ không?Câu trả lời ngắn gọn: Có!Được gọi bởi str (object) và định dạng hàm tích hợp () và print () để tính toán biểu diễn chuỗi không chính thức hoặc có thể in độc đáo của một đối tượng.Giá trị trả về phải là một đối tượng chuỗi.Yes! Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

Phương pháp __ trong Python được gọi là gì?

Phương pháp ma thuật trong Python là các phương pháp đặc biệt bắt đầu và kết thúc với các dấu gạch dưới kép.Chúng cũng được gọi là phương pháp dunder.dunder methods.

__ Lớp __ có nghĩa là gì trong Python?

__ class__ là một thuộc tính trên đối tượng đề cập đến lớp mà từ đó đối tượng được tạo.một.__Class__ # Đầu ra: b.__Class__ # Đầu ra: Sau các loại dữ liệu đơn giản, giờ đây chúng ta hãy hiểu chức năng loại và __Class__ với sự trợ giúp của lớp do người dùng xác định, con người.an attribute on the object that refers to the class from which the object was created. a. __class__ # Output: b. __class__ # Output: After simple data types, let's now understand the type function and __class__ attribute with the help of a user-defined class, Human .