Iterator basis

First, the definition

  1.iterator. (Iterators)

  2.iterable. (Iterable)

    In general, it can be a for loop that can be iterated, the iterator will be able iteration, iteration type but may not necessarily iterator.

Second, explain

  1. There __iter__ methods described may iterate

DEF shili1 ():
     Print (dir ([]))     
 # tell me all the list of methods have 
    Print (dir (()))
     Print (dir ( '' ))
     Print (dir (the Range (10 ))) 

    RET = the SET (the dir ([])) & SET (the dir ( '' )) & SET (the dir ({})) & SET (the dir (Range (10 )))
     Print (RET)    
 # a '__iter__'

   These determine what type of method did not have __iter__

print('__init__' in dir(int))
print('__init__' in dir(list))
print('__init__' in dir(dict))
print('__init__' in dir(set))
print('__init__' in dir(tuple))
print('__init__' in dir(enumerate))
print('__init__' in dir(range(10)))
 Print ( " __iter__ "  In dir (int))            # falsely 
print ( " __iter__ "  In dir (bool))      #false
 print ( " __iter__ "  In dir (list))
 print ( " __iter__ "  In dir (Diet) )
 print ( " __iter__ "  in dir (set))
 print ( " __iter__ "  in dir (tuple))
 print ( "__iter__ "  In dir (enumerate))
 print ( " __iter__ "  In dir (range (10)))

  2. #setstate specified taken from that starting position (typically from 0 bit first)

   An iterative type performed iter return value is an iterator to the method of

print(set(dir([].__iter__())) - set(dir([])))
# {'__setstate__', '__next__', '__length_hint__'}

  3. Example 1 (may not necessarily be iterative iterators, iterators must be iterative)

Print ( ' * ' * 40 ) 
L = [l, 2,3 ] 
Iterator = L. the __iter__ ()
 Print (Iterator. __next__ ())    # L no next internal method that returns the next value after the call has ITER 
Print (Iterator . __next__ ())    # L internal no next method, after returning the calls iter values are next 
Print (Iterator. __next__ ())    # L no internal next, the return value after calls iter have next 
# Print (Iterator .__ next __ ( )) given # 
# can not be recycled: not iterable (non-iterative) -> the __iter__ -> iter long as it contains can be iterative, and can for loop -> may be iteratively protocol

  4. Create an iterator

# [] .__ ITER __ () to get a 
# may iteration protocol 
# when __iter__ method for loop go first, if not 
# iterator protocol -> next method comprising inner and __init__ method is iterator 
Print ( ' the __iter__ '  in the dir ([]. the __iter__ ())) 

from Collections Import the Iterable
 from Collections Import the Iterator 

Print (the isinstance ([], the Iterator))   # isinstace determines whether or Yes. Iterator, iterator type. List is not an iterator, but may iteration. 
Print (the isinstance ([], the Iterable)) 

class A:
     # DEF the __init __ (Self): Pass 
    DEF  __next__ (Self): Pass      # While next and init iterators. next can be a an acquired value 
    # DEF the __iter __ (Self): Pass # have iter can be used iteratively 

A = A ()
 Print (the isinstance (A, the Iterable))
 Print (the isinstance (A, the Iterator)) 

# As long as the iterator must iteration may be not necessarily iterator iteration 
# may be iterative .__ init __ () method can be obtained an iterator

Third, the role of
  the benefits of using an iterator is to save memory space

# Benefits of using iterator: a value from one type of container, all values will take to 
# save memory space (iterator and will not take up a chunk of memory, but as the cycle, every time a generation, each next to a 
# Range, file handles 
# RANGE-> 
Print (Range (100000000))    # Range (0, 100000000) to give iterables, but this case does not generate data 
Print (List (Range (10000000000000 )))     # memory error 
Print (List (Range (10)))     # [0,1,2, ......,. 9] 

# F = Open -> 


# not for loop, can still traverse 
l = [1,2, 3,4- ] 
Iterator = L. the __iter__ ()
 the while True:
     Print (Iterator. __next__ ()) #While it is given, but also outputs a final throw StopIterator

 IV Summary

  1. Double Down: Method rarely called directly, usually triggered by other syntax.

  2. Can iterative - protocol may be iteratively: __iter__ the method comprising ( '__iter__' in dir (data))

  3. must be iterative loop may be for

  4. iterator protocol: Method and __next__ method comprising __iter__

  The iterator can be certain iteration, the iteration may be obtained by calling an iterator method __iter__

  6. iterator features:

    1) easy to use, and can take all the data fetched once. Re-take the next time.

    2) save memory space (current data and removing a)

Guess you like

Origin www.cnblogs.com/lowislucifer/p/10993694.html
Recommended