Importing an output from another function
James Stroud
jstroud at ucla.edu
Fri Mar 17 16:28:29 EST 2006
Byte wrote:
> Now what do I do if Func1() has multiple outputs and Func2() requires
> them all to give its own output, as follows:
>
> import random
>
> def Func1():
> choice = ('A', 'B', 'C')
> output = random.choice(choice)
> output2 = random.choice(choice)
> return output
> return output2
The function will return at "return output", so "return output2" will
never be reached.
> def Func2(item1, item2):
> print item1, item2
>
> output1 = Func1()
> Func2(output1)
>
> Thanks in advance,
> -- /usr/bin/byte
>
Try this (I think its called "argument expansion", but I really don't
know what its called, so I can't point you to docs):
def Func1():
choice = ('A', 'B', 'C')
output = random.choice(choice)
output2 = random.choice(choice)
return output, output2
def Func2(*items):
print items
output = Func1()
Func2(*output1)
BETTER:
=======
You can also make a "generator" (which I have made generalized, which
seems to be what you are striving for):
def Gener1(num):
choice = ('A', 'B', 'C')
for i in xrange(num):
yield random.choice(choice)
def Func2(item):
print item
for item in Gener1(2):
Func2(item)
James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com/
More information about the Python-list
mailing list