obtaining multiple values from a function.
Peter Otten
__peter__ at web.de
Tue Sep 25 04:27:00 EDT 2007
Shriphani wrote:
> If I have a function that loops over a few elements and is expected to
> throw out a few tuples as the output, then what should I be using in
> place of return ?
Use a generator and have it /yield/ rather than /return/ results:
>>> def f(items):
... for item in items:
... yield item.isdigit(), item.isupper()
...
>>> for result in f("AAA bbb 111".split()):
... print result
...
(False, True)
(False, False)
(True, False)
This is a very memory-efficient approach, particularly if the input items
are created on the fly, e. g. read from a file one at a time.
Peter
More information about the Python-list
mailing list