英文文档:

oct(x)
Convert an integer number to an octal 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.
说明:
  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的更多相关文章

  1. Python内置函数(19)——oct

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

  2. Python内置函数(46)——format

    英文文档: format(value[, format_spec]) Convert a value to a "formatted" representation, as con ...

  3. 【Python】Python内置函数dir详解

    1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码代码如下: >>> help(dir)Help on built-in function ...

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

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

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

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

  6. 16.python内置函数

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

  7. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  8. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  9. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

随机推荐

  1. MBR分区操作-增加、扩展、删除

    MBR分区操作-增加.扩展.删除 GPT分区参考 http://www.blogjava.net/haha1903/archive/2011/12/21/366942.html l  fdisk 显示 ...

  2. Netty5客户端源码解析

    Netty5客户端源码解析 今天来分析下netty5的客户端源码,示例代码如下: import io.netty.bootstrap.Bootstrap; import io.netty.channe ...

  3. unity 时间转换方式

    第一种  计时器的写法 带有调用系统时间 using System; using System.Collections; using System.Collections.Generic; using ...

  4. flink连接hbase方法及遇到的问题

    1.继承 RichSinkFunction 类 mvn配置: <dependency> <groupId>org.apache.flink</groupId> &l ...

  5. Linux从入门到放弃(为做一个开发+运维的全能性人才而奋斗)

    Linux?听说是一个操作系统,好用吗?” “我也不知道呀,和windows有什么区别?我能在Linux上玩LOL吗” “别提了,我用过Linux,就是黑乎乎一个屏幕,鼠标也不能用,不停地的敲键盘,手 ...

  6. 第三方npm包安装失败

    最近升级一些第三方库,老是出现无法正常安装npm的现象. 一.问题现象 1.webpack4安装失败 // 报错信息 webpack Maximum call stack size exceeded ...

  7. Stream闪亮登场

    Stream闪亮登场 一. Stream(流)是什么,干什么 Stream是一类用于替代对集合操作的工具类+Lambda式编程,他可以替代现有的遍历.过滤.求和.求最值.排序.转换等 二. Strea ...

  8. Vue 随机分配的打扫卫生H5 :打扫让我快乐

    情况是这样子的,每周四是我们小组打扫卫生,一共有四件活,7个人分配. 活分别是 : 扫地 拖地 倒垃圾 擦桌子 人分别是: '军', '春', '龙', '东', '贤', '磊','卿' 但是,每次 ...

  9. jQuery 动态绑定插件livequery的用法

  10. mybatis 之数据库 include refid ="base_column_list"

    mybatis 之数据库 include refid ="base_column_list" 对于刚学习使用SSM框架的新手来说,mybatis中的数据库语句有点不一样,下面便是对 ...