Python内置函数学习(5)-bin(x)

来源:互联网 发布:秋风知落叶 编辑:程序博客网 时间:2024/06/05 11:12

英文解释:
Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an index() method that returns an integer.
New in version 2.6.
中文翻译:
将一个整形数字转换为二进制字符串,结果是一个合法的python表达式,如果x不是一个整形对象,x对象必须有一个index()函数并且要返回整形

常用的各种进制类型的整形都可以应用bin函数

>>> bin(9)'0b1001'>>> bin(07)'0b111'>>> bin(0xa)'0b1010'

其他类型带有index()的也可以用bin(),不带的则不可以

>>> class A:    def __index__(self):        return 100>>> a=A()>>> bin(a)'0b1100100'>>> b="hello">>> bin(b)Traceback (most recent call last):  File "<pyshell#42>", line 1, in <module>    bin(b)TypeError: 'str' object cannot be interpreted as an index>>> 
0 0
原创粉丝点击