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 ========================== ...
随机推荐
- Vue2.0 demo:百度百聘第三方web客户端
github地址:https://github.com/axel10/baipin_vue 项目地址:https://vcollection.org/baipin/ 官方的百度百聘客户端存在翻页时过滤 ...
- Django 2.0 学习(03):Django视图和URL(下)
接上篇博文,继续分析Django基本流程. 编写第一个(view)视图函数 1.打开文件polls/views.py,输入下面的Python代码: from django.http import Ht ...
- Redis 基础(一)
Remote Dictionary Server(Redis)是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI C语言编写.遵守BSD ...
- call是什么?一次说个明白
首先简单粗暴的从例子中看概念 var a = {}; function foo() { this.name = "hello"; this.age = 100 } foo.call ...
- Maven 项目生成jar运行时提示“没有主清单属性”
新建了一个Maven的项目,mvn compile和mvn package后生成了jar文件,然后直接到target目录下去执行java -jar xxx.jar命令运行jar包时出现了"x ...
- ES6之promise的使用
let checkLogin = function () { return new Promise(function (resolve,reject) { let flag = document.co ...
- spring Boot+spring Cloud实现微服务详细教程第一篇
前些天项目组的大佬跟我聊,说项目组想从之前的架构上剥离出来公用的模块做微服务的开发,恰好去年的5/6月份在上家公司学习了国内开源的dubbo+zookeeper实现的微服务的架构.自己平时对微服务的设 ...
- 用golang 实现一个代理池
背景 写爬虫的时候总会遇到爬取速度过快而被封IP的情况,这个时候就需要使用代理了.在https://github.com/henson/ProxyPool 的启发下,决定自己实现一个代理池.项目已经开 ...
- Druid数据库连接池就这么简单
前言 本章节主要讲解Druid数据库连接池,为什么要学Druid数据库连接池呢?? 我的知识储备数据库连接池有两种->C3P0,DBCP,可是现在看起来并不够用阿~当时学习C3P0的时候,觉得这 ...
- npm scripts 使用指南
转载自:http://www.ruanyifeng.com/blog/2016/10/npm_scripts.html Node 开发离不开 npm,而脚本功能是 npm 最强大.最常用的功能之一. ...