Tràn ngăn xếp python tuple

Đôi khi, trong khi làm việc với Python, chúng ta có thể gặp sự cố khi cần kiểm tra xem một biến là một hay một bản ghi. Điều này có các ứng dụng trong các miền mà chúng tôi cần hạn chế loại dữ liệu mà chúng tôi làm việc trên đó. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.  

Show

    Phương pháp số 1. Sử dụng type() Hàm sẵn có này có thể được sử dụng làm tốc ký để thực hiện tác vụ này. Nó kiểm tra loại biến và cũng có thể được sử dụng để kiểm tra bộ dữ liệu.  

    Python3




    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    6

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    7

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    8

     

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    9

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    0
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    2
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    0
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    2
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    4
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    5

     

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    6

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    7____52____09
    ['true']
    0
    ['true']
    1
    ['true']
    2

     

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    7

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    8

    ['true']
    5
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    ['true']
    7_______98
    ['true']
    9
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    60

     

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    61

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    7____52____464
    ['true']
    0
    ['true']
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    67

    đầu ra.

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True

    Phương pháp #2. Sử dụng isinstance()

    Tuy nhiên, một chức năng khác có thể được sử dụng để thực hiện nhiệm vụ này. Nó cũng trả về true, trong trường hợp lớp cha của biến (nếu tồn tại) là một bộ.  

    Python3




    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    6

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    7

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    70

     

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    9

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    0
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    2
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    0
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    2
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    4
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    5

     

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    6

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    7____52____09
    ['true']
    0
    ['true']
    1
    ['true']
    2

     

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    7

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    70

    ['true']
    5
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    92
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    93
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    60
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    5

     

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    61

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    7____52____464
    ['true']
    0
    ['true']
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    67

    đầu ra.

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True

    Phương pháp. Sử dụng hiểu danh sách

    Python3




    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    0
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    2
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    0
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    2
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    4
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    5

    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    12
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    14
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    15
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    16
    ['true']
    7
    ['true']
    8
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    1
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    60
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    22
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    23
    The original tuple : (4, 5, 6)
    Is variable tuple ? : True
    24