Return a string result with out breaking loop

cnb circularfunc at yahoo.se
Mon Aug 25 21:02:15 EDT 2008


On Aug 26, 2:50 am, cnb <circularf... at yahoo.se> wrote:
> def somefunc():
>        for action, files in results:
>                full_filename = os.path.join(path_to_watch, files)
>                theact = ACTIONS.get(action, "Unknown")
>                yield  str(full_filename) +  " " + str(theact)
>
> ?
>
> Here is an example if that doesn't work, using yield, to show how to
> use yield:
> def yi(x):
>         while x > 0:
>                 yield str(x)
>                 x -= 1
>
> >>> yi(4)
>
> <generator object at 0x01FA3C88>
>
> >>> a=yi(4)
> >>> a.next()
> '4'
> >>> a.next()
> '3'
> >>> a.next()
> '2'
> >>> a.next()
> '1'
> >>> a.next()
>
> Traceback (most recent call last):
>   File "<pyshell#151>", line 1, in <module>
>     a.next()
> StopIteration
>
>
>
>


you can also do:
def yi(x):
	while x > 0:
		yield str(x)
		x -= 1
>>> a = yi(10)
>>> for x in a:
	print x


10
9
8
7
6
5
4
3
2
1
>>>



More information about the Python-list mailing list