isinstance方法判断可迭代和迭代器

from collections import Iterable
print(isinstance([],Iterable))
print(isinstance( {}, Iterable))
print(isinstance( (), Iterable))
print(isinstance( 'abc', Iterable))
print(isinstance( '100', Iterable))
print(isinstance((x for x in range(10) ), Iterable))


'''
True
D:/见解/Python/Python代码/vacation/python高级/使用isinstance判断是否可以迭代.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working
True
  from collections import Iterable
True
True
True
True
'''

from collections import Iterator
print(isinstance( [ ], Iterator))
print(isinstance(  'abc', Iterator))
print(isinstance(()  , Iterator))
print(isinstance( {} , Iterator))
print(isinstance(  123, Iterator))
print(isinstance(  5+2j, Iterator))
print(isinstance( (x for x in range(10)) , Iterator))
# 生成器可以是迭代器


'''
False
D:/见解/Python/Python代码/vacation/python高级/使用isinstance判断是否是迭代器.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working
False
  from collections import Iterator
False
False
False
False
True
'''

2020-05-08

posted @ 2020-05-08 00:30  Hany47315  阅读(969)  评论(0编辑  收藏  举报