Posted: Thu Jul 14, 2005 4:39 am Post subject: [Python-tut] Iterating Over any Object
So we've seen how we can do this in Ruby. It's pretty darn simple.
code:
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.
code:
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.
code:
bob = Name("Bob", "Smith")
And iterate over its component names.
code:
for name in bob:
print name
Sponsor Sponsor
MysticVegeta
Posted: Fri Jul 29, 2005 7:44 am Post subject: (No subject)
Isnt iteration calling of a same procedure inside that procedure in turing?
code:
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
Posted: Fri Jul 29, 2005 8:47 am Post subject: (No subject)
That's recrusion, MysticVegeta.
MysticVegeta
Posted: Fri Jul 29, 2005 9:10 pm Post subject: (No subject)
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
Posted: Fri Jul 29, 2005 9:36 pm Post subject: (No subject)
If it says that, it's wrong.
Hikaru79
Posted: Fri Jul 29, 2005 9:48 pm Post subject: (No subject)
MysticVegeta wrote:
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.