[Tutor] Explanation of this lambda

Eric Brunson brunson at brunson.com
Thu Mar 1 23:17:24 CET 2007


Johan Geldenhuys wrote:
> Thanks, Alan.
>
> That makes it a lot easier to understand. I'll play around with other
> examples to see what it is doing.
>
> Johan 
>   

A good first step to understanding a confusing lambda is to convert it 
to a normal function.\

Just my $.02,
e.

> -----Original Message-----
> From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
> Of Alan Gauld
> Sent: 22 February 2007 11:46 AM
> To: tutor at python.org
> Subject: Re: [Tutor] Explanation of this lambda
>
>
> "Johan Geldenhuys" <johan at accesstel.co.za> wrote
>
>   
>> Would somebody care to explain what is happening in this process?
>>
>> def intToBin(self, x, count=8):
>>        return "".join(map(lambda y:str((x>>y)&1), range(count-1, -1, 
>> -1)))
>>     
>
> "".join()   turns a list into a string
> map() returns a list where each item is the result of applying the lambda to
> the range()
>
> lamda y: .....  is a function to be applied
>
> str() converts to a string
>
> x>>y   shifts the bits of x right by y places  010 -> 001
>
> & 1 bitwise ands with 1 which returns 1 if the last bit is one.
>
> put the bits together and it should be clear.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   



More information about the Tutor mailing list