/log

life.log()

yield statement .. by example

with 2 comments

السلام عليكم و رحمة الله وبركاته

ده تمرين بسيط علىyield statement

def simpleGenerator( anyList ):
	for item in anyList:
		yield item

myList = ['Ahmed', 'AlSayed', 'AbdulHameed', 'Osama', 'Gamal']

nameGenerator =  simpleGenerator( myList )

for name in myItemsList:
	print name

print myItemsList.next()
print myItemsList.next()
print myItemsList.next()
وده بس عشان البلوج كان عشش فيه العنكبوت ..

Written by me

June 27th, 2010 at 9:45 pm

Posted in I code,software

Tagged with ,

2 Responses to 'yield statement .. by example'

Subscribe to comments with RSS or TrackBack to 'yield statement .. by example'.

  1. Is there a difference between both examples?

    Mahmoud

    5 Jul 10 at 2:55 AM

  2. No, I’m just showing that the for construct will be calling the next() method on the returned generator each iteration..

    It’s like range() and xrange()..

    me

    5 Jul 10 at 8:32 AM

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes