rdm at rcblue.com
Tue Jul 1 15:33:04 CEST 2008
At 06:23 AM 7/1/2008, Cédric Lucantis wrote:
>Le Tuesday 01 July 2008 15:04:11 Dick Moores, vous avez écrit :
> > At 05:43 AM 7/1/2008, Tim Golden wrote:
> > >Dick Moores wrote:
> > >>So I want to randomly choose between them. I thought that I might
> > >>be able to use choice() to do that. So,
> > >> (bunch of functions here)
> > >>if __name__ == '__main__':
> > >> choice([use_for_float_demo(), use_for_integer_demo()])
> > >>I find that what's gets run each time is BOTH of these functions,
> > >>first use_for_float_demo(), and then use_for_integer_demo()! What's
> > >> going on?
> > >
> > >What you *want* to do is choose one of two functions, and call
> > >whichever is chosen:
> > >
> > >fn = choice ([a, b])
> > >result = fn ()
> > >
> > >What you're *actually* doing is calling two functions, and returning
> > >one result or the other:
> > >
> > >result = choice ([a (), b ()])
> > I'm not sure I understand the distinction. It seems you're saying in
> > either case I get one result or the other. In fact, each time I run
> > the program, I get BOTH results.
>You _see_ both results because _you_ call both
>functions. choice() has nothing
>to do with that. Here's an example:
> >>> def foo1() : print 'running foo1'; return 'bar1'
> >>> def foo2() : print 'running foo2'; return 'bar2'
> >>> [foo1(), foo2()]
> >>> [foo1, foo2]
>[<function foo1 at 0x2b88257fc140>, <function foo2 at 0x2b88257fc230>]
>Do you see the difference now ?
Finally, I do. Thanks very much.
>In the first case you call the two functions
>and store their returned value in the list, while in the second you only
>store the function objects themselves without calling them. You want the
More information about the Tutor