
-----------------------------------
wtd
Thu Jul 14, 2005 4:39 am

[Python-tut] Iterating Over any Object
-----------------------------------
So we've seen how we can do this in Ruby.  It's pretty darn simple.

class Name
   attr_reader :first, :last

   def initialize(first, last)
      @first, @last = first, last
   end
  
   def each
      yield @first
      yield @last
   end
end

Well, as it turns out, it's pretty easy in Python too.

class Name(object):
   def __init__(self, first, last):
      self.__first, self.__last = first, last
   first = property(lambda self: self.first)
   last  = property(lambda self: self.last)
   def __iter__(self):
      yield self.__first
      yield self.__last

We can now create a new Name object.

bob = Name("Bob", "Smith")

And iterate over its component names.

for name in bob:
   print name

-----------------------------------
MysticVegeta
Fri Jul 29, 2005 7:44 am


-----------------------------------
Isnt iteration calling of a same procedure inside that procedure in turing?
proc something (x : int, y : int)
if y = 1 then
return
else
something(x, y-1)
end if
end something


Is that what you are talking about?

-----------------------------------
Mazer
Fri Jul 29, 2005 8:47 am


-----------------------------------
That's recrusion, MysticVegeta.

-----------------------------------
MysticVegeta
Fri Jul 29, 2005 9:10 pm


-----------------------------------
But the turing book says calling a procedure in that procedure is called iteration and calling a function inside the same function is called recursion...  :?

-----------------------------------
1of42
Fri Jul 29, 2005 9:36 pm


-----------------------------------
If it says that, it's wrong.

-----------------------------------
Hikaru79
Fri Jul 29, 2005 9:48 pm


-----------------------------------
But the turing book says calling a procedure in that procedure is called iteration and calling a function inside the same function is called recursion...  :?
Iteration is going through a set or list or array, etc, or objects/values/etc, in a sequential manner. Now, what the book might have given is a recursive example of iteration, which is entirely possible. However, there is a distinction between the two.
