Iterables
Literal meaning: it can be a real value cycle update.
Professional angle: interior includes
'__iter__'
object method, the object may be iterative.Determine whether an object is iterable:
'__iter__'
in dir (object)str list tuple dict set range
advantage:
Stored data can be displayed directly, more intuitive.
The method has more convenient operation.
Disadvantages:
used internal memory.
We can not be directly through the for loop, not directly value (index, key).
Iterator
Defined iterator
Literally: Update iteration: Tools: Tools iteration can be updated.
Professional angle: interior includes
'__iter__'
methods and containing'__next__'
an object method is iterator.May determine whether the iterator is:
'__iter__'
and'__next__'
in the absence dir (object)
Determine whether an object is an iterator
1 with open('文件1',encoding='utf-8',mode='w') as f1: 2 print(('__iter__' in dir(f1)) and ('__next__' in dir(f1))) 3