How can I make a function equal to 0?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Mar 22 00:32:28 EDT 2008


On 21 mar, 19:25, John Machin <sjmac... at lexicon.net> wrote:
> 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.

In that case, we could use a bare item() because nonzero would have
filtered out all that zeroes.

--
Gabriel Genellina



More information about the Python-list mailing list