Python内置函数(46)——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.
- 说明:
- 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('') # 字符串不能转换成8进制
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
oct('')
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内置函数(46)——oct的更多相关文章
- Python内置函数(19)——oct
英文文档: oct(x) Convert an integer number to an octal string. The result is a valid Python expression. ...
- Python内置函数(46)——format
英文文档: format(value[, format_spec]) Convert a value to a "formatted" representation, as con ...
- 【Python】Python内置函数dir详解
1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码代码如下: >>> help(dir)Help on built-in function ...
- Python之路(第八篇)Python内置函数、zip()、max()、min()
一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...
- Python之路Python内置函数、zip()、max()、min()
Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...
- 16.python内置函数
Python 内置函数:https://www.runoob.com/python/python-built-in-functions.html 原文:https://www.cnblogs.com/ ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
随机推荐
- JMeter通过beanShell脚本生成随机手机号
package xnzx; /** * @author xn088587 * */ public class getTel{ public static int getNum(int start,in ...
- Linux系统下配置网络、JAVA环境,配置tomcat,mysql
一.配置网络 1.进入自己的系统,并跳转到network-scripts 2.编辑 3.查看系统的信息 4.将其添加到刚刚的if-cfg-eth0中 5.重启网络 6.这个时候ping百度还是ping ...
- SpringBoot配置文件
一.配置文件 配置文件应该是无论在哪个框架中都是一个重要角色,而我们最为常用的xxx.xml和xxx.properties,还有springboot推荐使用的xxx.yml. 二.SpringBoot ...
- docker安装,无法正常启动
报错信息: Job for docker.service failed because the control process exited with error code. See "sy ...
- 3、java面向对象编程
1.面向对象内存分析 栈的特点 (1)JVM为每个线程创建一个栈,用于存放该线程执行方法的信息(实际参数.局部变量等) (2)栈属于线程私有,不能实现线程间的共享! (3)栈的存储特性是:先进后出,后 ...
- async与defer
<script>元素的几种常见属性: async 异步加载,立即下载,不应妨碍页面其他操作,标记为 async 的异步脚本并不保证按照指定的先后顺序执行,因此异步脚本不应该在加载期间修改 ...
- Charles抓包软件简介
Charles简介: Charles是一款抓包神器,因为他是基于 java 开发的,所以跨平台,Mac.Linux.Window下都是可以使用的,确保安装之前已经安装了JDK.Charles官网地址: ...
- Go语言基础之反射
Go语言基础之反射 本文介绍了Go语言反射的意义和基本使用. 变量的内在机制 Go语言中的变量是分为两部分的: 类型信息:预先定义好的元信息. 值信息:程序运行过程中可动态变化的. 反射介绍 反射是指 ...
- python获取多线程的返回值
import threading class MyThread(threading.Thread): def __init__(self,func,args=()): super(MyThread,s ...
- 《SQL优化入门》讲座总结
MySQL运行机制 MySQL每个query只能运行在一个CPU上,更多的CPU,更快的CPU会更有利于并发 MySQL执行计划 Using filesort: 表示无法利用索引完成排序,也有可能是因 ...