[Tutor] Function for converting ints from base10 to base2?

Terry Carroll carroll at tjc.com
Thu Feb 22 03:45:58 CET 2007


Taking your points out of order....

On Thu, 22 Feb 2007, John Fouhy wrote:

> Hmm, and it may not be faster either:

That doesn't surprise me.  It must have all the logic of the if-else 
construct, and additionally must build a list.

> It's more compact, but less clear (IMO), so I'm not a big fan.

I used it primarily because it mirrored my thinking; I don't know what
that says about me!  That's how I've often implemented the trinary 
operator.  I would have used 

   sign = '-' if n<0 else ''

But I'm still on Python 2.4.  I guess

   sign = (n<0) and '-' or ''

is the pre-2.5 idiom, but I find that even less clear.




More information about the Tutor mailing list