1

I was analysing code as I had some free time and thought could learn something new! This was from a Binary to Denary converter code I found. But I have been bouncing it around in my head for almost the past 2-3 days now but I absolutely can't bring myself to what this statement/function/line does or means?

lambda b: str(int(b, 2))

I think the 'b' in (b, 2) means Binary?

I researched and found that lambda is a function to 'call in' Anonymous functions or functions that have no name. Is that correct?

What does 'b:' mean? Is that also Binary or maybe a 'sub name' within lambda?

Also what does

str(int(b, 2))

mean? I know thats the code that converts binary to denary but how does it exactly work?

I am very curious to find out! Hopefully someone will be able to help me get my head around this!

PythonBeginner
  • 169
  • 4
  • 4
  • 12

3 Answers3

2

b is the argument to the lambda function:

lambda b: ....

The first part before the : defines the arguments for the anonymous function, this one defines just b just like a function signature would:

def binary_to_int(b):
    # ...

It could have been given another name too:

lambda anothername: int(anothername, 2)

It is bound to the argument you pass to this lambda:

binary_to_int = lambda b: int(b, 2)
result = binary_to_int('010101')  # so b is bound to '010101'

The function returns the result of the function int(b, 2), which interprets a string first argument as an integer in base 2 (binary):

If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

Here is a great question about lambdas: Why are Python lambdas useful?

int() from the docs:

int(x, base=10)

Convert a number or string x to an integer, or return 0 if no arguments are given. If x is a number, it can be a plain integer, a long integer, or a floating point number. If x is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.

If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.

Community
  • 1
  • 1
e h
  • 8,435
  • 7
  • 40
  • 58
1

b in this case is the argument to your anonymous lambda function. However, judging from what the anonymous function does, it seems pretty clear that b should be a binary string.

I think the easiest way to understand what the anonymous function does is through an example.

>>> b = '101'
>>> int(b, 2) # converts a binary string to base-10.
5
>>> str(int(b, 2))
'5'

The anonymous function converts a binary string to base-10 integer, and then converts that integer back to a string. To actually use the anonymous function, you could do something like the following:

>>> fn = lambda b: str(int(b, 2))
>>> fn('10001')
'17'

Or in de-anonymized form:

>>> def convertFromBinary(b): return str(int(b, 2))
>>> convertFromBinary('10001')
'17'
mdml
  • 22,442
  • 8
  • 58
  • 66