[Tutor] random.choice()

Dick Moores rdm at rcblue.com
Wed Jul 2 10:12:31 CEST 2008


At 12:54 AM 7/2/2008, wesley chun wrote:
>ok, someone has to be the bad guy and show an example of equivalent
>code that's more difficult to read:
>
>choice([use_for_float_demo, use_for_integer_demo])()
>
>seriously tho, the bottom line of what people have been telling you is
>that for the function (or method) foo():
>
>def foo():
>      :
>
>there is a distinction between:
>
>foo and foo()
>
>in the former, you have a function object.  it's just like any other
>Python object, but with one heaping distinction:  it's callable --
>this means that u can slap on a pair of parentheses after the object
>and execute it, which is what i did after calling choice() above to
>pick one of the 2 functions, then *calling it* with the trailing "()".
>
>in the latter, you've not only picked out a function object, but have
>also executed it as well.  that's why when you had choice([a(), b()]),
>you see the results/output from a() and b() -- you called both, got
>both return values, and asked choice() to pick one of the 2 return
>values!! and since you throw away the return value, it didn't matter
>which one came back because both had executed.

Thanks, Wes, for the further clarity.

Dick




More information about the Tutor mailing list