Hướng dẫn are numbers passed by value python? - các số được truyền bởi giá trị 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; Show Python3
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 string = 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 True0 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 True3 Inside Function [10, 20, 30, 40, 50] Outside Function: [10, 20, 30, 40, 50]9 Output Inside Function: GeeksforGeeks Outside Function: Geeks Ví dụ 2 & nbsp; & nbsp; Python3
110001234557894 110001234557894 True6 110001234557894 110001234557894 True7 110001234557894 110001234557894 True8 Inside Function [10, 20, 30, 40, 50] Outside Function: [10, 20, 30, 40, 50]1 110001234557894 110001234557894 True7 541190289536222 541190288737777 False1 541190289536222 541190288737777 False2 541190289536222 541190288737777 False3 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 541190289536222 541190288737777 False7 541190289536222 541190288737777 False8 110001234557894 110001234557894 True7 541190289536222 541190288737777 False3 Inside Function: new value Outside Function: old value1 = Inside Function: new value Outside Function: old value3 Inside Function: new value Outside Function: old value4 Inside Function: new value Outside Function: old value5 Inside Function: new value Outside Function: old value6 Inside Function: new value Outside Function: old value5 Inside Function: new value Outside Function: old value8 Inside Function: new value Outside Function: old value5 ['Nothing, 'how', 'are', 'you', 'doing']0____ ['Nothing, 'how', 'are', 'you', 'doing']1 ['Nothing, 'how', 'are', 'you', 'doing']2 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 True3 ['Nothing, 'how', 'are', 'you', 'doing']6 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ượngTrong 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. Python3['Nothing, 'how', 'are', 'you', 'doing']7 = ['Nothing, 'how', 'are', 'you', 'doing']9
['Nothing, 'how', 'are', 'you', 'doing']9 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 string 5string 6Inside 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 string 5= 0Inside Function [10, 20, 30, 40, 50] Outside Function: [10, 20, 30, 40, 50]6 = 2= 3 = 4Output 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; Python3['Nothing, 'how', 'are', 'you', 'doing']7 = Inside Function: new value Outside Function: old value3 Inside Function: new value Outside Function: old value4 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value6 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value8 ['Nothing, 'how', 'are', 'you', 'doing']1
Inside Function: new value Outside Function: old value3 Inside Function: new value Outside Function: old value4 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value6 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value8 ['Nothing, 'how', 'are', 'you', 'doing']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 string 5string 6Inside 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 string 5= 0Inside Function [10, 20, 30, 40, 50] Outside Function: [10, 20, 30, 40, 50]6 = 2= 3 = 4Output 541190289536222 541190288737777 False 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. Python3['Nothing, 'how', 'are', 'you', 'doing']7 = Inside Function: new value Outside Function: old value3 Inside Function: new value Outside Function: old value4 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value6 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value8 ['Nothing, 'how', 'are', 'you', 'doing']1
Inside Function: new value Outside Function: old value3 Inside Function: new value Outside Function: old value4 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value6 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value8 ['Nothing, 'how', 'are', 'you', 'doing']1 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 True3 Inside Function [10, 20, 30, 40, 50] Outside Function: [10, 20, 30, 40, 50]9 Output: Inside Function: new value Outside Function: old value
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. Python3['Nothing, 'how', 'are', 'you', 'doing']7 = Inside Function: new value Outside Function: old value3 Inside Function: new value Outside Function: old value4 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value6 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value8 ['Nothing, 'how', 'are', 'you', 'doing']1
Inside Function: new value Outside Function: old value3 Inside Function: new value Outside Function: old value4 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value6 541190289536222 541190288737777 False8 Inside Function: new value Outside Function: old value8 ['Nothing, 'how', 'are', 'you', 'doing']1 Đầ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: ['Nothing, 'how', 'are', 'you', 'doing']
Inside Function [10, 20, 30, 40, 50] Outside Function: [10, 20, 30, 40, 50]06 |