obtaining multiple values from a function.

S

Shriphani

hello all,

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 ?
Shriphani Palakodety.
 
P

Peter Otten

Shriphani said:
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:
.... for item in items:
.... yield item.isdigit(), item.isupper()
.... .... 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
 
B

Ben Finney

Shriphani said:
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 ?

If it makes sense for the set of results to be returned all at once,
then return the object that collects them all together — a list of
tuples, for example.


If, instead, it makes sense for the results to be iterated over, you
can write a function that yields results one at a time, without
necessarily knowing in advance what the entire set will be::
... """ Yield numbers in the Fibonacci sequence
... to a maximum value of max_result. """
... prev_results = [0, 0]
... result = 1
... while result < max_result:
... yield result
... prev_results = [prev_results[1], result]
... result = sum(prev_results)
...

The function, when called, will return a generator object that you can
either iterate over::
... print n
...
1
1
2
3
5
8
13
21
34
55
89

or directly call its 'next' method to get one result at a time until
it raises a 'StopIteration' exception::
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
 
P

Paul Rudin

If, instead, it makes sense for the results to be iterated over, you
can write a function that yields results one at a time, without
necessarily knowing in advance what the entire set will be::

Going off on a tangent a bit, but I was idly considering the absence
of itertools.ireduce the other day. A problem is that reduce gives a
single result, so it's not clear what ireduce would do (perhaps why
it's not there). One obvious candidate would be to give the whole
sequence of partial reductions. I'm not sure if this would be
generally useful, but it would give a succinct way to do the Fibonacci
sequence:

itertools.ireduce(operator.add, itertools.count())
 
C

Carsten Haese

Going off on a tangent a bit, but I was idly considering the absence
of itertools.ireduce the other day. A problem is that reduce gives a
single result, so it's not clear what ireduce would do (perhaps why
it's not there). One obvious candidate would be to give the whole
sequence of partial reductions. I'm not sure if this would be
generally useful, but it would give a succinct way to do the Fibonacci
sequence:

itertools.ireduce(operator.add, itertools.count())

Let's try it:
.... for nxt in iterable:
.... partial = op(partial, nxt)
.... yield partial
.... .... print x
....
0
1
3
6
10
15
21
28
36
45

That's not the Fibonacci sequence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top