Đặt trong python có thể thay đổi hoặc không thay đổi

Mỗi biến trong python chứa một thể hiện của một đối tượng. Có hai loại đối tượng trong python i. e. Các đối tượng có thể thay đổi và không thể thay đổi. Bất cứ khi nào một đối tượng được khởi tạo, nó sẽ được gán một id đối tượng duy nhất. Loại đối tượng được xác định trong thời gian chạy và nó không thể thay đổi sau đó. Tuy nhiên, trạng thái của nó có thể được thay đổi nếu nó là một đối tượng có thể thay đổi

Để tóm tắt sự khác biệt, các đối tượng có thể thay đổi có thể thay đổi trạng thái hoặc nội dung của chúng và các đối tượng không thể thay đổi không thể thay đổi trạng thái hoặc nội dung của chúng

  • Đối tượng bất biến. Đây là các loại dựng sẵn như int, float, bool, string, unicode, tuple. Nói một cách đơn giản, một đối tượng bất biến không thể thay đổi sau khi nó được tạo




    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    9

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    2
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    3
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    2
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    6
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    7

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    8
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    00
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    3
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    02

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    03
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    04

    Lỗi

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    4




    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    9

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    06

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    07

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    08
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    3
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    80

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    81
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0______100
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    3
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    85

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    03
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    87

    Lỗi

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
  • Đối tượng có thể thay đổi. Đây là các loại list, dict, set. Các lớp tùy chỉnh thường có thể thay đổi




    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    9

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    89

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    90_______43
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    92____393
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    95
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    1
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    97
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    00

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    03
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    00

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    07

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    02
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    00
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    3
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    06

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    02
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    08
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    2
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    00
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    3
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    12

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    03
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    00

    đầu ra

    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    0

Phần kết luận

  1. Các đối tượng có thể thay đổi và không thể thay đổi được xử lý khác nhau trong python. Các đối tượng bất biến truy cập nhanh hơn và tốn kém để thay đổi vì nó liên quan đến việc tạo một bản sao.
    Trong khi các đối tượng có thể thay đổi thì dễ dàng thay đổi.
  2. Nên sử dụng các đối tượng có thể thay đổi khi có nhu cầu thay đổi kích thước hoặc nội dung của đối tượng
  3. Ngoại lệ. Tuy nhiên, cũng có một ngoại lệ trong tính bất biến. Chúng tôi biết rằng tuple trong python là bất biến. Nhưng bộ dữ liệu bao gồm một chuỗi các tên có liên kết không thể thay đổi đối với các đối tượng.
    Xem xét một tuple
    Traceback [most recent call last]:
      File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in 
        message[0] = 'p'
    TypeError: 'str' object does not support item assignment
    8

    Tuple bao gồm một chuỗi và một danh sách. Chuỗi là bất biến nên chúng ta không thể thay đổi giá trị của nó. Nhưng nội dung của danh sách có thể thay đổi. Bản thân bộ dữ liệu không thể thay đổi nhưng chứa các mục có thể thay đổi

Theo nguyên tắc thông thường, các loại giống Nguyên thủy nói chung có thể là bất biến và các loại giống Vùng chứa tùy chỉnh hầu hết có thể thay đổi

Bộ [] trong Python là gì?

set[] được dùng để chuyển đổi bất kỳ phần tử có thể lặp nào thành chuỗi các phần tử có thể lặp với các phần tử riêng biệt , thường được gọi là Set. Thông số. Bất kỳ chuỗi có thể lặp lại nào như danh sách, bộ dữ liệu hoặc từ điển. trả lại. Một tập rỗng nếu không có phần tử nào được truyền.

Các bộ có thể thay đổi được không?

Bộ là tập hợp các mục không có thứ tự. Mỗi phần tử tập hợp là duy nhất [không trùng lặp] và phải bất biến [ không thể thay đổi ].

Được đặt trong Python không có thứ tự?

Các tập hợp không có thứ tự . Các phần tử của tập hợp là duy nhất. Các yếu tố trùng lặp không được phép. Bản thân một tập hợp có thể được sửa đổi, nhưng các phần tử chứa trong tập hợp phải thuộc loại bất biến.

Chủ Đề