英文文档:

bin(x)

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.

  将整数转换为2进制格式字符串

说明:

1. 将一个整形数字转换成二进制字符串

>>> b = bin(3)
>>> b
'0b11'
>>> type(b) #获取b的类型
<class 'str'>

2. 如果参数x不是一个整数,则x必须定义一个 __index__() 方法,并且方法返回值必须是整数。

2.1 如果对象不是整数,则报错

>>> class A:
pass >>> a = A()
>>> bin(a)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
bin(a)
TypeError: 'A' object cannot be interpreted as an integer

2.2 如果对象定义了__index__方法,但返回值不是整数,报错

>>> class B:
def __index__(self):
return "3" >>> b = B()
>>> bin(b)
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
bin(b)
TypeError: __index__ returned non-int (type str)

2.3 对象定义了__index__方法,且返回值是整数,将__index__方法返回值转换成二进制字符串

>>> class C:
def __index__(self):
return 3 >>> c = C()
>>> bin(c)
'0b11'

Python内置函数(18)——bin的更多相关文章

  1. Python内置函数(5)——bin

    英文文档: bin(x) Convert an integer number to a binary string. The result is a valid Python expression. ...

  2. Python内置函数(18)——enumerate

    英文文档: enumerate(iterable, start=0) Return an enumerate object. iterable must be a sequence, an itera ...

  3. Python内置函数进制转换的用法

    使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...

  4. Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)

    使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...

  5. 16.python内置函数

    Python 内置函数:https://www.runoob.com/python/python-built-in-functions.html 原文:https://www.cnblogs.com/ ...

  6. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

  7. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

  8. 【286】◀▶ Python 内置函数说明

    参考: Python 内置函数 01   abs() 返回数字的绝对值. 02   all() 用于判断给定的可迭代参数 iterable 中的所有元素是否不为 0.''.False 或者 itera ...

  9. Python之路Python内置函数、zip()、max()、min()

    Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...

随机推荐

  1. Spring Data(二)查询

    Spring Data(二)查询 接着上一篇,我们继续讲解Spring Data查询的策略. 查询的生成 查询的构建机制对于Spring Data的基础是非常有用的.构建的机制将截断前缀find-By ...

  2. Java关键字汇总

    Java共有51个关键字和2个保留字,保留字可能在后面的版本中成为新的关键字.关键字均为小写. 2个保留字: const:用于修改字段或局部变量的声明.它指定字段或局部变量的值是常数,不能被修改 go ...

  3. ServiceFabric极简文档-1.0 Service Fabric 自定义集群部署

    Service Fabric 部署集群:https://docs.microsoft.com/zh-cn/azure/service-fabric/service-fabric-get-started ...

  4. 纯js生成验证码

    实现代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"& ...

  5. Cesium 一个导致浏览器内存一直增长的方法

    为了实时更改模型的位置,给模型附上ID,后面判断如果传来的数据中没有已经创建的模型,删掉该模型时用到方法:viewer.entities.removeById(modelId);和viewer.ent ...

  6. mybatis返回list

    1 Model类 public class Vo { /** * this is used for receive data partly from table user_question_secti ...

  7. Node.JS开发环境准备

    1.安装Nodejs的Windows包. 官网:http://nodejs.org/ 2.可以使用cmd运行nodejs项目,命令格式: node  文件名.js node  文件名 3.对于不熟悉的 ...

  8. New FileReader上传图片

    function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader() ...

  9. Oracle/Hive/Impala SQL比较1

    5 Function      指数据库内置的function,不讨论UDF.另外,操作符都不比较了,区别不大.   5.1 数学函数 功能 Oracle Hive Impala ABS 绝对值,有 ...

  10. 【Linux】积累笔记

    ■ 关于查看系统的一些版本信息 查看系统的发行版本可以用 cat /etc/issue 或者 cat /etc/redhat-release (Centos上) 查看系统的内核版本以及系统位数 una ...