Hướng dẫn call-by sharing python - cuộc gọi bằng cách chia sẻ python

Python sử dụng một hệ thống, được gọi là Call Call by the Object tham khảo đối tượng hoặc cuộc gọi của người khác. Trong trường hợp bạn chuyển các đối số như toàn bộ số, chuỗi hoặc bộ dữ liệu cho một hàm, việc vượt qua giống như giá trị cuộc gọi vì bạn không thể thay đổi giá trị của các đối tượng bất biến được truyền đến hàm. Trong khi việc truyền các đối tượng có thể thay đổi có thể được coi là cuộc gọi bằng tham chiếu bởi vì khi các giá trị của chúng được thay đổi bên trong hàm, thì nó cũng sẽ được phản ánh bên ngoài hàm. Ví dụ 1: & nbsp; & nbsp;Example 1:  
Example 1: 
 

Nội dung chính ShowShow

  • Gọi theo giá trị với ví dụ là gì?
  • Cuộc gọi theo giá trị và cuộc gọi bằng cách tham khảo trong Python là gì?
  • Tại sao cuộc gọi theo giá trị được gọi là cuộc gọi theo giá trị?
  • Sự khác biệt giữa cuộc gọi theo giá trị và cuộc gọi theo địa chỉ với ví dụ là gì?

Python3

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
22
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
24

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
25

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
0
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
1
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
22
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
4
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
1
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
8
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
9
110001234557894
110001234557894
True
0
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
110001234557894
110001234557894
True
3
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
9

Output     
 

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
3

Ví dụ 2 & nbsp; & nbsp;   
 

Python3

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
25

110001234557894
110001234557894
True
6
110001234557894
110001234557894
True
7
110001234557894
110001234557894
True
8
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
1
110001234557894
110001234557894
True
7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
91
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
92
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
93
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
1
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
97
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
110001234557894
110001234557894
True
7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
93
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
91
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
93
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
94
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
95
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
96
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
95
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
95
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
70____
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
71
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
72
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
110001234557894
110001234557894
True
3
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
76

Output   
 

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]

Liên kết tên với các đối tượng

Trong Python, mỗi biến mà chúng tôi gán một giá trị/container được coi là một đối tượng. Khi chúng tôi gán một giá trị cho một biến, chúng tôi thực sự ràng buộc một tên với một đối tượng. & Nbsp;binding a name to an object. binding a name to an object.
 

Python3

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
77
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
79

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
220
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
79
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
225
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
226
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
225
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
230
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
232
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
233
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
234

Output      
 

110001234557894
110001234557894
True

Bây giờ, hãy để thử và hiểu điều này tốt hơn với một ví dụ khác. Ví dụ 2: & nbsp;Example 2: 
Example 2:
 

Python3

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
77
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
93
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
94
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
96
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
71

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
220
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
93
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
94
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
96
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
71
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
225
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
226
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
225
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
230
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
232
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
233
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
234

Output     
 

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
9

Bây giờ, hãy để thử và hiểu điều này tốt hơn với một ví dụ khác. Ví dụ 2: & nbsp;mutable and the string is immutable. An immutable variable cannot be changed once created. If we wish to change an immutable variable, such as a string, we must create a new instance and bind the variable to the new instance. Whereas, mutable variable can be changed in place.Example 3:  mutable and the string is immutable. An immutable variable cannot be changed once created. If we wish to change an immutable variable, such as a string, we must create a new instance and bind the variable to the new instance. Whereas, mutable variable can be changed in place.
Example 3: 
 

Python3

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7 7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
93
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
94
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
96
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
71

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
220
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
93
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
94
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
96
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
71
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
1
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
8
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
15

Đầu ra của hai ví dụ trên là khác nhau vì danh sách có thể thay đổi và chuỗi là bất biến. Một biến bất biến không thể được thay đổi sau khi tạo. Nếu chúng ta muốn thay đổi một biến bất biến, chẳng hạn như một chuỗi, chúng ta phải tạo một thể hiện mới và liên kết biến với thể hiện mới. Trong khi đó, biến có thể thay đổi có thể được thay đổi tại chỗ. Ví dụ 3: & nbsp; & nbsp;

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
19
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7
110001234557894
110001234557894
True
3
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
9

Output:  
 

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
9

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
25

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
06immutable type of object is passed as argument to the function foo. Within the scope of the given function foo, a= “new value” has been bounded to the same object that string has been bound outside. Within the scope of the function foo, we modify “old value”` to “new value”. Once we leave the scope of function foo , a=”new value” is no longer in the name space, and the value that string refers to was never changed.Example 4: Now, let us look at how mutable variable is passed into the function. immutable type of object is passed as argument to the function foo. Within the scope of the given function foo, a= “new value” has been bounded to the same object that string has been bound outside. Within the scope of the function foo, we modify “old value”` to “new value”. Once we leave the scope of function foo , a=”new value” is no longer in the name space, and the value that string refers to was never changed.
Example 4: Now, let us look at how mutable variable is passed into the function.
 

Python3

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
77
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
93
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
94
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
96
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
71

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
220
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
23

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
93
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
94
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
96
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
98
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
71

Đầu ra của hai ví dụ trên là khác nhau vì danh sách có thể thay đổi và chuỗi là bất biến. Một biến bất biến không thể được thay đổi sau khi tạo. Nếu chúng ta muốn thay đổi một biến bất biến, chẳng hạn như một chuỗi, chúng ta phải tạo một thể hiện mới và liên kết biến với thể hiện mới. Trong khi đó, biến có thể thay đổi có thể được thay đổi tại chỗ. Ví dụ 3: & nbsp; & nbsp;

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
45
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
6
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
47

Output:     
 

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
7

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
25

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]
06 Gọi theo giá trị với ví dụ là gì?
 

Gọi theo giá trị với ví dụ là gì?

Cuộc gọi theo phương thức giá trị chuyển các đối số đến một hàm sao chép giá trị thực của một đối số vào tham số chính thức của hàm. Trong trường hợp này, các thay đổi được thực hiện cho tham số bên trong hàm không có tác dụng đối với đối số. Theo mặc định, C Lập trình C sử dụng cuộc gọi theo giá trị để truyền đối số.copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments.copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments.

Cuộc gọi theo giá trị và cuộc gọi bằng cách tham khảo trong Python là gì?

Trong cuộc gọi theo giá trị, một tham số hoạt động trong hàm như một biến cục bộ mới được khởi tạo thành giá trị của đối số (một bản sao cục bộ (bị cô lập) của đối số). Trong cuộc gọi bằng tham chiếu, biến đối số do người gọi cung cấp có thể bị ảnh hưởng bởi các hành động trong hàm được gọi.

Tại sao cuộc gọi theo giá trị được gọi là cuộc gọi theo giá trị?

Trong khi gọi một hàm, khi bạn truyền các giá trị bằng cách sao chép các biến, nó được gọi là cuộc gọi của các giá trị.Trong khi gọi một hàm, trong ngôn ngữ lập trình thay vì sao chép các giá trị của các biến, địa chỉ của các biến được sử dụng, nó được gọi là cuộc gọi của các tài liệu tham khảo.Trong phương pháp này, một bản sao của biến được truyền.when you pass values by copying variables, it is known as “Call By Values.” While calling a function, in programming language instead of copying the values of variables, the address of the variables is used it is known as “Call By References. In this method, a copy of the variable is passed.when you pass values by copying variables, it is known as “Call By Values.” While calling a function, in programming language instead of copying the values of variables, the address of the variables is used it is known as “Call By References. In this method, a copy of the variable is passed.

Sự khác biệt giữa cuộc gọi theo giá trị và cuộc gọi theo địa chỉ với ví dụ là gì?

Trong cuộc gọi theo giá trị, các giá trị của các tham số thực tế sao chép vào các tham số chính thức của hàm.Tuy nhiên, trong cuộc gọi theo địa chỉ, các địa chỉ của các tham số thực tế sao chép vào tham số chính thức của hàm.Do đó, chức năng là sự khác biệt chính giữa cuộc gọi theo giá trị và cuộc gọi theo địa chỉ.