First, iterator iterators
An iterator is only a container object, and it implements the iterator protocol. It has two basic methods:
1) Next method
Returns the next element of a container
2) __iter__ method
Returns the iterator itself
Iterators can be created using the built-in ITER method, see examples:
Copy Code code as follows:
>>> i = ITER (' abc ')
>>> I.next ()
A
>>> I.next ()
' B '
>>> I.next ()
C
>>> I.next ()
Traceback (most recent call last):
File "<string>", line 1, in <string>
Stopiteration:
Class Myiterator (object):
def __init__ (self, Step):
Self.step = Step
Def next (self):
"" "Returns the next element." "
If self.step==0:
Raise Stopiteration
Self.step-=1
Return Self.step
def __iter__ (self):
"" "Returns the iterator itself." "
return self
For El in Myiterator (4):
Print El
--------------------
Results:
Copy Code code as follows:
Second, generator generators
From Python2.2, the builder provides a neat way to help return a list element's functions to complete simple and efficient code.
It is based on the yield instruction, allowing the STOP function and returning the result immediately.
This function saves its execution context and, if necessary, continues execution immediately.
For example, the Fibonacci function:
Copy Code code as follows:
Def Fibonacci ():
a,b=0,1
While True:
Yield b
A,b = B, a+b
Fib=fibonacci ()
Print Fib.next ()
Print Fib.next ()
Print Fib.next ()
Print [Fib.next () for I in range (10)]
--------------------
Results:
Copy Code code as follows:
1
1
2
[3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
PEP python enhancement Proposal python enhancements recommendations
Tokenize Module
Copy Code code as follows:
>>> Import Tokenize
>>> reader = open (' c:/temp/py1.py '). Next
>>> Tokens=tokenize.generate_tokens (reader)
>>> Tokens.next ()
(1, ' class ', (1, 0), (1, 5), ' Class Myiterator (object):/n ')
>>> Tokens.next ()
(1, ' Myiterator ', (1, 6), (1), ' Class Myiterator (object):/n ')
>>> Tokens.next ()
(Wuyi, ' (1), (1), ' Class Myiterator (object):/n ')
Example:
Copy Code code as follows:
def power (values):
For value in values:
print ' powering%s '%value
Yield value
def adder (values):
For value in values:
print ' Adding to%s '%value
If value%2==0:
Yield value+3
Else
Yield value+2
elements = [1,4,7,9,12,19]
res = adder (Power (elements))
Print Res.next ()
Print Res.next ()
--------------------
Results:
Copy Code code as follows:
Powering 1
Adding to 1
3
Powering 4
Adding to 4
7
Keep the code simple, not the data.
Note: It is better to have a large number of simple iterative functions than a complex function that computes only one value at a time.
Example:
Copy Code code as follows:
Def psychologist ():
print ' Please tell me your problems '
While True:
Answer = (yield)
If answer is not None:
If Answer.endswith ('? '):
Print ("Don t ask yourself too much questions")
Elif ' good ' in answer:
Print "A" good, go on "
Elif ' bad ' in answer:
Print "Don ' is so negative"
Free = psychologist ()
Print Free.next ()
Print Free.send (' I feel bad ')
Print Free.send ("Why I shouldn ' t?")
Print Free.send ("OK then I should find what are good for me")
--------------------
Results:
Copy Code code as follows:
Please tell me your problems
None
Don ' t be so negative
None
Don ' t ask yourself too much questions
None
A that's good, go on
None