[Tutor] Explanation of this lambda

Alan Gauld alan.gauld at btinternet.com
Thu Feb 22 10:46:21 CET 2007


"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




More information about the Tutor mailing list