[cse491] some code equivalencies

C. Titus Brown ctb at msu.edu
Wed Sep 3 13:11:39 PDT 2008


Hi all,

this might help a bit:

----
for a in x:
   print a
----

is functionally almost equivalent to

----
iter_obj = x.__iter__()

while 1:
   a = iter_obj.next()
   print a
----

(The only difference is that 'next()' raises a StopIteration exception
that is caught invisibly in the first, but shows up in the second
example.)

Also,

---
y = list(x)
---

is exactly equivalent to

---
y = []
for a in x:
   y.append(a)
---

cheers,
--titus
-- 
C. Titus Brown, ctb at msu.edu



More information about the cse491-fall-2008 mailing list