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;
Example 1: 
 

Show

    Python3

    string = "Geeks"

    def

    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]
    1string =
    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: GeeksforGeeks
    Outside Function: Geeks

    Ví dụ 2 & nbsp; & nbsp; 
     

    Python3

    def

    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
    541190289536222
    541190288737777
    False
    1
    541190289536222
    541190288737777
    False
    2
    541190289536222
    541190288737777
    False
    3

    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
    False
    7
    541190289536222
    541190288737777
    False
    8
    110001234557894
    110001234557894
    True
    7
    541190289536222
    541190288737777
    False
    3

    Inside Function: new value
    Outside Function: old value
    1=
    Inside Function: new value
    Outside Function: old value
    3
    Inside Function: new value
    Outside Function: old value
    4
    Inside Function: new value
    Outside Function: old value
    5
    Inside Function: new value
    Outside Function: old value
    6
    Inside Function: new value
    Outside Function: old value
    5
    Inside Function: new value
    Outside Function: old value
    8
    Inside Function: new value
    Outside Function: old value
    5
    ['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
    True
    3
    ['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ượ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.
     

    Python3

    ['Nothing, 'how', 'are', 'you', 'doing']
    7=
    ['Nothing, 'how', 'are', 'you', 'doing']
    9

    string 0=

    ['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]
    7string 5string 6

    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]
    7string 5=0

    Inside Function [10, 20, 30, 40, 50]
    Outside Function: [10, 20, 30, 40, 50]
    6=2=3 =4

    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:
     

    Python3

    ['Nothing, 'how', 'are', 'you', 'doing']
    7=
    Inside Function: new value
    Outside Function: old value
    3
    Inside Function: new value
    Outside Function: old value
    4
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    6
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    8
    ['Nothing, 'how', 'are', 'you', 'doing']
    1

    string 0=

    Inside Function: new value
    Outside Function: old value
    3
    Inside Function: new value
    Outside Function: old value
    4
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    6
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    8
    ['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]
    7string 5string 6

    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]
    7string 5=0

    Inside Function [10, 20, 30, 40, 50]
    Outside Function: [10, 20, 30, 40, 50]
    6=2=3 =4

    Output   
     

    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.
    Example 3: 
     

    Python3

    ['Nothing, 'how', 'are', 'you', 'doing']
    7=
    Inside Function: new value
    Outside Function: old value
    3
    Inside Function: new value
    Outside Function: old value
    4
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    6
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    8
    ['Nothing, 'how', 'are', 'you', 'doing']
    1

    string 0=

    Inside Function: new value
    Outside Function: old value
    3
    Inside Function: new value
    Outside Function: old value
    4
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    6
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    8
    ['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
    True
    3
    Inside Function [10, 20, 30, 40, 50]
    Outside Function: [10, 20, 30, 40, 50]
    9

    Output: 
     

    Inside Function: new value
    Outside Function: old value

    def

    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 value
    3
    Inside Function: new value
    Outside Function: old value
    4
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    6
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    8
    ['Nothing, 'how', 'are', 'you', 'doing']
    1

    string 0=

    Inside Function: new value
    Outside Function: old value
    3
    Inside Function: new value
    Outside Function: old value
    4
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    6
    541190289536222
    541190288737777
    False
    8
    Inside Function: new value
    Outside Function: old value
    8
    ['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']

    def

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