bin(x)
返回前綴為“0b”的二進制字符串.
說明
如果參數(shù) x 是整數(shù),函數(shù)返回 x 對應(yīng)的前綴為“0b”的二進制字符串;
如果 x 不是 int 對象,則 x 對象必須包含方法 __index__(),并且方法 __index__() 的返回值必須是一個整數(shù)。
示例
>>> bin(25)
'0b11001'
>>> bin(-25)
'-0b11001'
>>> # 非整型的情況
... class fooType:
... pass
...
>>> t=fooType()
>>> bin(t)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'fooType' object cannot be interpreted as an integer
>>>
>>>
>>> class fooType:
... def __index__(self):
... return 25
...
>>> t=fooType()
>>> bin(t)
'0b11001'