Inside Function: new value Outside Function: old value9
['Nothing, 'how', 'are', 'you', 'doing']3
['Nothing, 'how', 'are', 'you', 'doing']1
['Nothing, 'how', 'are', 'you', 'doing']2
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']4
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']6
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']8
['Nothing, 'how', 'are', 'you', 'doing']9
['Nothing, 'how', 'are', 'you', 'doing']40
['Nothing, 'how', 'are', 'you', 'doing']09
['Nothing, 'how', 'are', 'you', 'doing']10____118
['Nothing, 'how', 'are', 'you', 'doing']44
Đầu ra
['Nothing, 'how', 'are', 'you', 'doing']0
Ràng buộc tên đối tượng
Trong python, mỗi biến mà chúng ta gán giá trị/vùng chứa được coi là một đối tượng. Khi chúng ta gán một giá trị cho một biến, chúng ta thực sự đang gắn tên cho một đối tượng.
Python3
['Nothing, 'how', 'are', 'you', 'doing']45
['Nothing, 'how', 'are', 'you', 'doing']3
['Nothing, 'how', 'are', 'you', 'doing']47
['Nothing, 'how', 'are', 'you', 'doing']48
['Nothing, 'how', 'are', 'you', 'doing']3
['Nothing, 'how', 'are', 'you', 'doing']47
['Nothing, 'how', 'are', 'you', 'doing']021
['Nothing, 'how', 'are', 'you', 'doing']022
['Nothing, 'how', 'are', 'you', 'doing']09
['Nothing, 'how', 'are', 'you', 'doing']10____1025
['Nothing, 'how', 'are', 'you', 'doing']026
['Nothing, 'how', 'are', 'you', 'doing']021
['Nothing, 'how', 'are', 'you', 'doing']022
['Nothing, 'how', 'are', 'you', 'doing']09
['Nothing, 'how', 'are', 'you', 'doing']10____1025
['Nothing, 'how', 'are', 'you', 'doing']092
['Nothing, 'how', 'are', 'you', 'doing']093
['Nothing, 'how', 'are', 'you', 'doing']094
['Nothing, 'how', 'are', 'you', 'doing']09
['Nothing, 'how', 'are', 'you', 'doing']096______1097
['Nothing, 'how', 'are', 'you', 'doing']098
Đầu ra
['Nothing, 'how', 'are', 'you', 'doing']1
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.
Python3
['Nothing, 'how', 'are', 'you', 'doing']45
['Nothing, 'how', 'are', 'you', 'doing']3
['Nothing, 'how', 'are', 'you', 'doing']1_______12
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']4
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']6
['Nothing, 'how', 'are', 'you', 'doing']9
['Nothing, 'how', 'are', 'you', 'doing']48
['Nothing, 'how', 'are', 'you', 'doing']3
['Nothing, 'how', 'are', 'you', 'doing']1_______12
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']4
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']6
['Nothing, 'how', 'are', 'you', 'doing']9
['Nothing, 'how', 'are', 'you', 'doing']007
['Nothing, 'how', 'are', 'you', 'doing']008
['Nothing, 'how', 'are', 'you', 'doing']009
['Nothing, 'how', 'are', 'you', 'doing']09
['Nothing, 'how', 'are', 'you', 'doing']10____1025
['Nothing, 'how', 'are', 'you', 'doing']026
['Nothing, 'how', 'are', 'you', 'doing']007
['Nothing, 'how', 'are', 'you', 'doing']008
['Nothing, 'how', 'are', 'you', 'doing']009
['Nothing, 'how', 'are', 'you', 'doing']09
['Nothing, 'how', 'are', 'you', 'doing']10____1025
['Nothing, 'how', 'are', 'you', 'doing']092
['Nothing, 'how', 'are', 'you', 'doing']021
['Nothing, 'how', 'are', 'you', 'doing']022
['Nothing, 'how', 'are', 'you', 'doing']09
['Nothing, 'how', 'are', 'you', 'doing']096______1097
['Nothing, 'how', 'are', 'you', 'doing']098
Đầu ra
['Nothing, 'how', 'are', 'you', 'doing']7
Đầu ra của hai ví dụ trên khác nhau vì danh sách có thể thay đổi và chuỗi không thay đổi. Một biến bất biến không thể thay đổi sau khi được 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.
Python3
['Nothing, 'how', 'are', 'you', 'doing']01
['Nothing, 'how', 'are', 'you', 'doing']028
['Nothing, 'how', 'are', 'you', 'doing']03
['Nothing, 'how', 'are', 'you', 'doing']03
['Nothing, 'how', 'are', 'you', 'doing']031
['Nothing, 'how', 'are', 'you', 'doing']03
['Nothing, 'how', 'are', 'you', 'doing']033
['Nothing, 'how', 'are', 'you', 'doing']03
['Nothing, 'how', 'are', 'you', 'doing']45
['Nothing, 'how', 'are', 'you', 'doing']3
['Nothing, 'how', 'are', 'you', 'doing']037
['Nothing, 'how', 'are', 'you', 'doing']03
['Nothing, 'how', 'are', 'you', 'doing']09
['Nothing, 'how', 'are', 'you', 'doing']10
['Nothing, 'how', 'are', 'you', 'doing']11
['Nothing, 'how', 'are', 'you', 'doing']042
['Nothing, 'how', 'are', 'you', 'doing']03
['Nothing, 'how', 'are', 'you', 'doing']03
['Nothing, 'how', 'are', 'you', 'doing']14
________ 109 ________ 93 ________ 1048
['Nothing, 'how', 'are', 'you', 'doing']049
['Nothing, 'how', 'are', 'you', 'doing']09
['Nothing, 'how', 'are', 'you', 'doing']10____118
['Nothing, 'how', 'are', 'you', 'doing']12
Đầu ra.
Inside Function: new value Outside Function: old value
Trong ví dụ trên, một chuỗi là một loại đối tượng không thay đổi được truyền làm đối số cho hàm foo. Trong phạm vi của hàm đã cho foo, a= “giá trị mới” đã được liên kết với cùng một đối tượng mà chuỗi đã được liên kết bên ngoài. Trong phạm vi của hàm foo, chúng tôi sửa đổi “giá trị cũ”` thành “giá trị mới”. Khi chúng tôi rời khỏi phạm vi của chức năng foo , a=”new value” không còn trong không gian tên và giá trị mà chuỗi đề cập đến không bao giờ thay đổi.
Ví dụ 4. Bây giờ, chúng ta hãy xem biến có thể thay đổi được truyền vào hàm như thế nào.
Python3
['Nothing, 'how', 'are', 'you', 'doing']01
['Nothing, 'how', 'are', 'you', 'doing']028
['Nothing, 'how', 'are', 'you', 'doing']03
['Nothing, 'how', 'are', 'you', 'doing']057____1058
['Nothing, 'how', 'are', 'you', 'doing']9
['Nothing, 'how', 'are', 'you', 'doing']3
['Nothing, 'how', 'are', 'you', 'doing']061
['Nothing, 'how', 'are', 'you', 'doing']03
['Nothing, 'how', 'are', 'you', 'doing']063
['Nothing, 'how', 'are', 'you', 'doing']064
['Nothing, 'how', 'are', 'you', 'doing']3
['Nothing, 'how', 'are', 'you', 'doing']1
['Nothing, 'how', 'are', 'you', 'doing']067
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']069
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']071
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']073
Inside Function: new value Outside Function: old value5
['Nothing, 'how', 'are', 'you', 'doing']075
['Nothing, 'how', 'are', 'you', 'doing']9