英文文档:

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. 谷歌chrome 插件(扩展)开发——谈谈安装

    chrome extension  安装的方式简单,打包方式也是非常简单的. 官方给出了三种安装方式: Chrome Web Store:把你的扩展程序上传到Chrome  Web Store, &q ...

  2. 20.1章JSON语法

    1,语法 JSON有三种类型的值 简单值:使用与JavaScript相同的语法,可以在JSON中表示字符串,数值,布尔值,null.但是JSON不支持JavaScript中特殊的值undefined. ...

  3. 反射、Attribute

    1.发射是对类或者对象,查看其类内部的构造. 2.类的组成:属性(PropertyInfo).方法(MethodInfo).字段(FiedInfo).构造函数(ConstructorInfo).事件( ...

  4. myeclipse设置环境(最实用的教程)

    1. General --> Workspace --> UTF-82. General --> Editors --> Associations --> JSP --& ...

  5. 对JS prototype的理解

    1.什么是prototype? function F() {} f1 = new F();f2 = new F(); 以上的代码,F()是一个构造函数,f1和f2是由这个构造函数产生的对象. prot ...

  6. Java基于自定义注解的面向切面的实现

    目的:实现在任何想要切的地方添加一个注解就能实现面向切面编程 自定义注解类 @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retentio ...

  7. Redis 学习相关的网站

    Redis 命令参考 http://doc.redisfans.com/ https://redis.io/commands http://www.redis.net.cn Redis教程 http: ...

  8. 剑指Offer-孩子们的游戏(圆圈中最后剩下的数)

    package Other; import java.util.LinkedList; /** * 孩子们的游戏(圆圈中最后剩下的数) * 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友, ...

  9. Vue之八 HTML5 History模式

    nginx配置 location / { root /webroot/www/ShopMall3; try_files $uri $uri/ /index.html; } /:访问路径: root:服 ...

  10. Python的几个小程序,其实我觉得可以称作初学时的基础算法

    昨天学习的,今天做一下整理,以前学过几天c,感觉什么都没有搞出来,有点泄气,看到Python后试试,从最基本的东西学起,希望不要辜负我的这一点热情. if语句的应用 n=1 while n<5: ...