Iterable trong Python là gì

Nói một cách đơn giản, bất kỳ đối tượng nào có thể được lặp lại đều có thể hiểu được. Chẳng hạn, một đối tượng danh sách là có thể sử dụng được và một đối tượng STR cũng vậy. Các số danh sách và tên chuỗi là Iterables vì ​​chúng tôi có thể lặp lại chúng (sử dụng vòng lặp trong trường hợp này). & nbsp; Trong bài viết này, chúng ta sẽ xem cách kiểm tra xem một đối tượng có thể sử dụng được trong Python không. check if an object is iterable in Python.

Nội dung chính Show

  • Phương pháp 1: Sử dụng kiểm tra phương thức __iter__.
  • Phương pháp 2: Sử dụng lớp ITEBLE.
  • TypeError: 'int' object is not iterable3TypeError: 'int' object is not iterable4Roster is iterable9Roster is iterable0TypeError: 'int' object is not iterable8
  • Kiểm tra xem đối tượng có thực hiện __iter__ hay không và gọi nó để có được một trình lặp.
  • Là chuỗi có được không?
  • Những loại nào đó có thể hiểu được trong Python?
  • Điều gì không phải là có thể sử dụng trong Python?
  • Những loại dữ liệu nào không được xác định trong Python?

Examples: 

Đầu vào: "Xin chào" “Hello”

Đầu ra: Đối tượng không phải là điều đáng tin cậy object is not iterable

Giải thích: Đối tượng có thể giống như (H, E, L, L, O)Object could be like( h, e, l, l, o)

Đầu vào: 5 5

Đầu ra: đối tượng ’int không thể hiểu được ‘int’ object is not iterable

Ví dụ 1:

Python3

H e l l o
3
H e l l o
4
H e l l o
5

H e l l o
6

TypeError: 'int' object is not iterable
0
TypeError: 'int' object is not iterable
1
TypeError: 'int' object is not iterable
2
TypeError: 'int' object is not iterable
3
TypeError: 'int' object is not iterable
4
TypeError: 'int' object is not iterable
5
H e l l o
4
TypeError: 'int' object is not iterable
7
TypeError: 'int' object is not iterable
8

Output:

H e l l o

Ví dụ 2:

Python3

TypeError: 'int' object is not iterable
9
H e l l o
4
TypeError: 'int' object is not iterable
01

H e l l o
6

TypeError: 'int' object is not iterable
03
TypeError: 'int' object is not iterable
1
TypeError: 'int' object is not iterable
05
TypeError: 'int' object is not iterable
3
TypeError: 'int' object is not iterable
4
TypeError: 'int' object is not iterable
08

Đầu ra

TypeError: 'int' object is not iterable

Tuy nhiên, một đối tượng INT không thể hiểu được và một đối tượng nổi cũng vậy. Số đối tượng số nguyên không phải là điều đáng tin cậy, vì chúng ta không thể lặp lại nó.int object is not iterable and so is a float object. The integer object number is not iterable, as we are not able to loop over it.

Kiểm tra khả năng lặp lại của một đối tượng trong Python

Chúng ta sẽ khám phá các cách khác nhau để kiểm tra xem một đối tượng có thể có được hay không. Chúng tôi sử dụng hàm HasAttr () để kiểm tra xem tên đối tượng chuỗi có thuộc tính __iter__ để kiểm tra khả năng lặp lại hay không. Tuy nhiên, kiểm tra này không toàn diện.iterable or not. We use the hasattr() function to test whether the string object name has __iter__ attribute for checking iterability. However, this check is not comprehensive.

Phương pháp 1: Sử dụng kiểm tra phương thức __iter__.

Python3

H e l l o
3
H e l l o
4

TypeError: 'int' object is not iterable
01
TypeError: 'int' object is not iterable
02
TypeError: 'int' object is not iterable
03
TypeError: 'int' object is not iterable
04
TypeError: 'int' object is not iterable
05
TypeError: 'int' object is not iterable
06
TypeError: 'int' object is not iterable
3
TypeError: 'int' object is not iterable
4
TypeError: 'int' object is not iterable
09
TypeError: 'int' object is not iterable
00
TypeError: 'int' object is not iterable
8
TypeError: 'int' object is not iterable
02
TypeError: 'int' object is not iterable
03
TypeError: 'int' object is not iterable
3
TypeError: 'int' object is not iterable
4
TypeError: 'int' object is not iterable
09
TypeError: 'int' object is not iterable
07
TypeError: 'int' object is not iterable
8

Output:

TypeError: 'int' object is not iterable
0

Phương pháp 2: Sử dụng lớp ITEBLE.

Chúng tôi có thể xác minh rằng một đối tượng có thể sử dụng được bằng cách kiểm tra xem đó có phải là một ví dụ của lớp Iterable hay không. Kiểm tra cá thể báo cáo đối tượng chuỗi là điều chỉnh chính xác bằng cách sử dụng lớp ITBER. Điều này hoạt động cho Python 3 là tốt. Đối với Python 3.3 trở lên, bạn sẽ phải nhập lớp ITerable từ bộ sưu tập.abc chứ không phải từ các bộ sưu tập như vậy:collections.abc and not from collections like so:

Python3

TypeError: 'int' object is not iterable
09
TypeError: 'int' object is not iterable
80
TypeError: 'int' object is not iterable
81
TypeError: 'int' object is not iterable
82

H e l l o
3
H e l l o
4

TypeError: 'int' object is not iterable
01
TypeError: 'int' object is not iterable
02
TypeError: 'int' object is not iterable
03
TypeError: 'int' object is not iterable
04
TypeError: 'int' object is not iterable
05
TypeError: 'int' object is not iterable
06
TypeError: 'int' object is not iterable
3
TypeError: 'int' object is not iterable
4
TypeError: 'int' object is not iterable
09
H e l l o
32
TypeError: 'int' object is not iterable
8
TypeError: 'int' object is not iterable
02
TypeError: 'int' object is not iterable
03
TypeError: 'int' object is not iterable
3
TypeError: 'int' object is not iterable
4
TypeError: 'int' object is not iterable
09
H e l l o
39
TypeError: 'int' object is not iterable
8

Output:

TypeError: 'int' object is not iterable
0

TypeError: 'int' object is not iterable3TypeError: 'int' object is not iterable4Roster is iterable9Roster is iterable0TypeError: 'int' object is not iterable8

Phương pháp 2: Sử dụng lớp ITEBLE.iter built-in function works in the following way:

  • Chúng tôi có thể xác minh rằng một đối tượng có thể sử dụng được bằng cách kiểm tra xem đó có phải là một ví dụ của lớp Iterable hay không. Kiểm tra cá thể báo cáo đối tượng chuỗi là điều chỉnh chính xác bằng cách sử dụng lớp ITBER. Điều này hoạt động cho Python 3 là tốt. Đối với Python 3.3 trở lên, bạn sẽ phải nhập lớp ITerable từ bộ sưu tập.abc chứ không phải từ các bộ sưu tập như vậy:__iter__, and calls that to obtain an iterator.
  • TypeError: 'int' object is not iterable
    09
    TypeError: 'int' object is not iterable
    80
    TypeError: 'int' object is not iterable
    81
    TypeError: 'int' object is not iterable
    82TypeError, usually saying “C object is not iterable,” where C is the class of the target object.

Code:

Python3

TypeError: 'int' object is not iterable
02
TypeError: 'int' object is not iterable
87
TypeError: 'int' object is not iterable
88

H e l l o
44

TypeError: 'int' object is not iterable
03

Phương pháp 3: Sử dụng hàm tích hợp iter ().

TypeError: 'int' object is not iterable
3
TypeError: 'int' object is not iterable
4
H e l l o
51
H e l l o
52
H e l l o
53
H e l l o
54
H e l l o
55

Chức năng tích hợp ITER hoạt động theo cách sau:

TypeError: 'int' object is not iterable
3
TypeError: 'int' object is not iterable
4
H e l l o
51
H e l l o
61
H e l l o
53
H e l l o
54
H e l l o
55

Output:

TypeError: 'int' object is not iterable
0

Kiểm tra xem đối tượng có thực hiện __iter__ hay không và gọi nó để có được một trình lặp.

Nếu điều đó thất bại, Python sẽ tăng loại, thường nói rằng đối tượng C C không thể điều chỉnh được, trong đó C là lớp của đối tượng đích.

Python3

H e l l o
3
H e l l o
4
H e l l o
43

TypeError: 'int' object is not iterable
3
H e l l o
477 ____78
TypeError: 'int' object is not iterable
04
TypeError: 'int' object is not iterable
4
H e l l o
51
TypeError: 'int' object is not iterable
07
TypeError: 'int' object is not iterable
8
TypeError: 'int' object is not iterable
02
TypeError: 'int' object is not iterable
03
TypeError: 'int' object is not iterable
04
TypeError: 'int' object is not iterable
4
H e l l o
51
TypeError: 'int' object is not iterable
14
TypeError: 'int' object is not iterable
8

Output:

TypeError: 'int' object is not iterable
8

Là chuỗi có được không?

Ví dụ, chuỗi cũng có thể lặp lại. Nếu một đối tượng về mặt kỹ thuật không phải là một mảng, nhưng đại diện cho một bộ sưu tập (danh sách, bộ) của một cái gì đó, thì vì..ous là một cú pháp tuyệt vời để lặp qua nó, vì vậy hãy xem cách làm cho nó hoạt động.strings are also iterable. If an object isn't technically an array, but represents a collection (list, set) of something, then for..of is a great syntax to loop over it, so let's see how to make it work.

Những loại nào đó có thể hiểu được trong Python?

Ví dụ về các vòng lặp bao gồm tất cả các loại trình tự (như danh sách, str và tuple) và một số loại không theo trình tự như dict, đối tượng tệp và đối tượng của bất kỳ lớp nào bạn xác định bằng phương thức __iter __ () hoặc bằng phương thức __getItem __ () mà phương thức mà phương thức màthực hiện trình tự ngữ nghĩa.