Python内置函数(19)——oct
英文文档:
oct(x)- Convert an integer number to an octal string. The result is a valid Python expression. If x is not a Python
intobject, it has to define an__index__()method that returns an integer.
将整数转换为8进制的字符串
- 说明:
- 1. 函数功能将一个整数转换成8进制字符串。如果传入浮点数或者字符串均会报错。
>>> a = oct(10) >>> a
'0o12'
>>> type(a) # 返回结果类型是字符串
<class 'str'> >>> oct(10.0) # 浮点数不能转换成8进制
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
oct(10.0)
TypeError: 'float' object cannot be interpreted as an integer >>> oct('10') # 字符串不能转换成8进制
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
oct('10')
TypeError: 'str' object cannot be interpreted as an integer
2. 如果传入参数不是整数,则其必须是一个定义了__index__并返回整数函数的类的实例对象。
# 未定义__index__函数,不能转换
>>> class Student:
def __init__(self,name,age):
self.name = name
self.age = age >>> a = Student('Kim',10)
>>> oct(a)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
oct(a)
TypeError: 'Student' object cannot be interpreted as an integer # 定义了__index__函数,但是返回值不是int类型,不能转换
>>> class Student:
def __init__(self,name,age):
self.name = name
self.age = age
def __index__(self):
return self.name >>> a = Student('Kim',10)
>>> oct(a)
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
oct(a)
TypeError: __index__ returned non-int (type str) # 定义了__index__函数,而且返回值是int类型,能转换
>>> class Student:
def __init__(self,name,age):
self.name = name
self.age = age
def __index__(self):
return self.age >>> a = Student('Kim',10)
>>> oct(a)
'0o12'
Python内置函数(19)——oct的更多相关文章
- Python内置函数(19)——eval
英文文档: eval(expression, globals=None, locals=None) The arguments are a string and optional globals an ...
- Python内置函数(46)——oct
英文文档: oct(x) Convert an integer number to an octal string. The result is a valid Python expression. ...
- Python内置函数(19)-slice
官方文档 class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set o ...
- 【Python】Python内置函数dir详解
1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码代码如下: >>> help(dir)Help on built-in function ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- Python之路(第八篇)Python内置函数、zip()、max()、min()
一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...
- python内置函数和魔法函数
内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...
- Python之路Python内置函数、zip()、max()、min()
Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...
- Python内置函数6
Python内置函数6 1.license() 输出当前python 的license信息 A. HISTORY OF THE SOFTWARE ========================== ...
随机推荐
- angular2 实现的小项目
之前根据官网的demo做了一个小例子,将的都比较基本,为了更好的提高对angular的认知,又做了一个小例子,目前还不完善.主要有路由,http,组件之间的通信,服务等基本知识. 项目地址:https ...
- 用Canvas生成随机验证码(后端前端都可以)
一 .使用前端生成验证码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- vue 数组渲染问题
vue 数组渲染问题 问题一: 用v-for循环渲染数组数据时,数据更新了,视图却没有更新 由于 JavaScript 的限制, Vue 不能检测以下变动的数组: 1. 当你利用索引直接设置一个项时, ...
- 客户端用TortoiseSVN检出代码
在桌面空白处右键选择SVN检出,如图: 在弹出的对话框中填写服务端版本库的URL (URL获取方式在前面一章中介绍过)选择检出目录点击确定,如图: 开始检出项目,如图: 检出完成之后打开副本,即可看到 ...
- Online Judge(OJ)搭建——3、MVC架构
Model Model 层主要包含数据的类,这些数据一般是现实中的实体,所以,Model 层中类的定义常常和数据库 DDL 中的 create 语句类似. 通常数据库的表和类是一对一的关系,但是有的时 ...
- NEO从入门到开窗(1) - 一个智能合约的诞生
一.啰嗦两句 最近一直都在研究区块链,BitCoin,Etherenum, Hyper Ledger Fabric还有今天的主角小蚂蚁,当然出名以后改了一个艺名叫NEO.区块链大部分都是用Golang ...
- Docker国内镜像源设置
编辑json文件,添加如下内容后重启docker即可. [root@Docker ~]# cat /etc/docker/daemon.json{ "registry-mirrors&quo ...
- 剑指Offer-构建乘积数组
package Array; import sun.security.util.Length; /** * 构建乘积数组 * 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,... ...
- 笔记:Jersey REST API 设计
REST 统一接口 REST 使用 HTTP 协议的通用方法作为统一接口的标准词汇,REST 服务所提供的方法信息都在 HTTP 方法里,每一种HTTP请求方法都可以从安全性和幂等性两方面考虑,这对正 ...
- 笔记:Maven 插件配置 - maven-jar-plugin
在项目中内部重用某个模块的测试代码很常见的需求,可能某个底层模块的测试代码中包含一些常用的测试工具类,或者一些高质量的测试基类供继承,这个时候Maven用户就需要通过配置此插件将测试类打包,插件配置代 ...