How can I make a function equal to 0?

John Machin sjmachin at lexicon.net
Fri Mar 21 18:25:02 EDT 2008


On Mar 22, 8:51 am, Gabriel Genellina <gagsl-... at yahoo.com.ar> wrote:

> If you drop this condition then you could fill the array with zeroes
> as before, replace only the interesting ones with actual functions,
> and write:
>
> for item in myarray[nonzero(myarray)]:
>     print item() if callable(item) else item
>

Let's unbuckle the seat belt :-)

If "item" is definitely restricted to being either 0 or a callable
object, then
    item() if item else 0
should give the same answer as, and is quite likely to be faster than,
    item() if callable(item) else item
as it avoids a global lookup and a function call.

Cheers,
John



More information about the Python-list mailing list