为什么在 python 中使用 int() 将二进制转换为整数会在给出 2 作为基本参数时出错

Why does converting binary to integer using int() in python gives an error when give 2 as the base argument

我尝试了以下代码,

a=0b1101
b=int(a,2)
print(b)

它给了我一个错误。

TypeError: int() can't convert non-string with explicit base

但是当我没有提到基础时它工作正常,

a=0b1101
b=int(a)
print(b)

开头的零在 python 中具有特殊含义。例如。 0x 表示十六进制数。 在使用 int() 函数时,当二进制数以整数形式存储时,我们提供第二个参数。例如,

b=int(a,2)

但是在将实际二进制数转换为整数时,我们不提供第二个参数。例如,

b=int(a)