Importing an output from another function

Paul Rubin http
Sat Mar 18 02:43:24 CET 2006


"Byte" <eoinrogers at gmail.com> writes:
> Probably a stupid question, but I'm a newbie and this really pisses me
> off. Run this script:
> 
> import random
> 
> def Func1():
>     choice = ('A', 'B', 'C')
>     output = random.choice(choice)
> 
> def Func2():
>     print output
> 
> Func1()
> Func2()

You could declare output to be global, but it's kind of poor style.
Preferable is something like:

    def Func1():
        choice = ('A', 'B', 'C')
        output = random.choice(choice)
        return output

    def Func2(x):
        print x

    output = Func1()  # this "output" is not the same as the one in Func1
    Func2(output)



More information about the Python-list mailing list