Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Python-tut] Iterating Over any Object
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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
Sponsor
sponsor
MysticVegeta




PostPosted: 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




PostPosted: Fri Jul 29, 2005 8:47 am   Post subject: (No subject)

That's recrusion, MysticVegeta.
MysticVegeta




PostPosted: 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... Confused
1of42




PostPosted: Fri Jul 29, 2005 9:36 pm   Post subject: (No subject)

If it says that, it's wrong.
Hikaru79




PostPosted: 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... Confused

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.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: