How can I make a function equal to 0?

John Machin sjmachin at lexicon.net
Sat Mar 22 00:41:51 EDT 2008


On Mar 22, 3:32 pm, Gabriel Genellina <gagsl-... at yahoo.com.ar> wrote:
> 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.

True.




More information about the Python-list mailing list