iterator - Python: is iter(x) equivalent to for el in x: yield el? -
suppose there collection:
list = [1,2,3] is builtin method
it1 = iter(list) equivalent to
def niter(x): el in x: yield el it2 = niter(list) edit: to farther clarify, know iter() can except more arguments, in principal want know if same yield doing in niter
no. iter(lst) returns list_iterator object, sec illustration generic generator. both same, in different ways. xxx_iterator objects aware of construction iterating on, , utilize specific properties implement next method. generic generators don't know arguments (if any) , rely on them implement iterator protocol. iter(lst) returns real iterator, 1 fetches items list, , niter wrapper simply delegates job argument (which happens list_iterator 1 time again).
in other words iter(obj) says "dear obj, need knows how iterate you" , for z in obj "i don't care how iterate you, gimme values".
python iterator
No comments:
Post a Comment