1.python How to iterate an object
A. Cyclic version-iterator
Implemented by implementing the property method of the class
Class Fab (object):
def __init__ (self, max):
Self.max = max
SELF.N, SELF.A, self.b = 0, 0, 1
def __iter__ (self)://Return Iteration Properties
return self
Def next (self)://Implementing an iterative approach
If SELF.N < self.max:
r = self.b
SELF.A, self.b = self.b, SELF.A + self.b
SELF.N = SELF.N + 1
Return r
Raise Stopiteration ()
>>> for N in Fab (5):
... print N
...
Stopiteration exception thrown at end of traversal
ITER = (x**2 for x in rang (ten) if x%2==0) generates an iterator that is equivalent to yield
List = [x**2 for x in rang (ten) if x%2==0] Generate lists
B.yield-builder (also an iterator: an iterator that is automatically generated by the interpreter to help keep the code simple)
Def FAB (max):
N, a, b = 0, 0, 1
While n < max:
Yield b
A, B = B, A + b
n = n + 1
>>> for N in Fab (5):
... print N
...
The effect of yield is to turn a function into a generator, the function with yield is no longer a normal function, the Python interpreter treats it as a generator, and the Call to FAB (5) does not execute the FAB function, but instead returns a Iterable Object! When the For loop executes, each loop executes the code inside the FAB function, and when it executes to yield B, the FAB function returns an iteration value, and the next iteration, the code proceeds from the next statement of Yield B, and the local variable of the function looks exactly the same as before the last break, so the function Continue execution until yield is encountered again. You can also call the next () Method of Fab (5) Manually (because Fab (5) is a generator object that has the next () method)
>>> f = Fab (5)
>>> F.next ()
To distinguish between Fab and fab (5), Fab is a generator function, and fab (5) is a generator returned by a call to fab, like the definition of a class and the difference between an instance of a class
In a generator function, if there is no return, the default execution to the function is finished throwing stopiteration, if return in the execution process, then directly throws the stopiteration termination iteration.
Another example of yield is from file reads. Calling the Read () method directly on a file object causes unpredictable memory consumption. A good approach is to use fixed-length buffers to continuously read the contents of the file. With yield, we no longer need to write an iterative class of read files to easily implement file reads:
def read_file (Fpath):
Block_size = 1024
With open (Fpath, ' RB ') as F:
While True:
block = F.read (block_size)
If block:
Yield block
Else
Return
A generator or iterator can significantly reduce the overhead of memory compared to a list
For line in open ("Test.txt"): #use file iterators
Print Line
The place to save memory is to use generators (fast, save memory)
2. Adding object Members dynamically
Class Info ():
def __init__ (self):
self.a=10
>>info = info ()
>>info.b=20
>>print INFO.B #动态添加对象成员
This article from "Tech record" blog, declined reprint!
Some advanced syntax for Python