Distinguishing objects and iteration iterator

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:

    1. Stored data can be displayed directly, more intuitive.

    2. The method has more convenient operation.

  • Disadvantages:

    1. used internal memory.

    2. 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 

Guess you like

Origin www.cnblogs.com/fkdby/p/11124824.html