Print a string in binary format

Steven Bethard steven.bethard at gmail.com
Fri Jan 21 14:32:35 EST 2005


Stephen Thorne wrote:
> On Fri, 21 Jan 2005 01:54:34 GMT, Kartic
> <removethis.kartic.krishnamurthy at gmail.com> wrote:
>>Aha..I guess I posted too soon.
>>
>>You might want to take a look at this cookbook entry:
>>http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/219300
>>
>>Defines lambdas to convert integer to binary. The one you probably want is -
>> >>> bstr = lambda n, l=16: n<0 and binarystr((2L<<l)+n) or n and
>>bstr(n>>1).lstrip('0')+str(n&1) or '0'
>> >>> bstr(ord('a'))
>>'1100001'
> 
> 
> Death to inappropriate usage of lambda.
> First of all, that lambda is buggy, it doesn't work for negative
> numbers, but you can't immediately see that because of the compressed
> nature of the code.
>
[snip how to write better code without lambdas]

Your discussion here was nicely illustrative.  You might consider adding 
something to the "Overuse of Lambda" discussion in:

http://www.python.org/moin/DubiousPython

Steve



More information about the Python-list mailing list