• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python内置函数OCT详解

Python内置函数OCT详解

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python oct函数,python oct,oct函数,oct,oct是几月份等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

英文文档:

Convert an integer number to an octal string. The result is a valid Python expression. If x is not a Pythonobject, it has to define anmethod that returns an integer. </div>

说明:

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

</div>

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'
</div>

</div>

您可能想查找下面的文章:

  • Python内置函数OCT详解

相关文章

  • python完成FizzBuzzWhizz问题(拉勾网面试题)示例
  • Python fileinput模块使用实例
  • python中执行shell的两种方法总结
  • Python选课系统开发程序
  • 学习python 之编写简单乘法运算题
  • python变量不能以数字打头详解
  • Python中__name__的使用实例
  • python中使用urllib2获取http请求状态码的代码例子
  • 使用Python下载Bing图片(代码)
  • Python中的jquery PyQuery库使用小结

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python字典的常用操作方法小结
    • 使用Python脚本将文字转换为图片的实例分享
    • Python实现定时任务
    • 在 Python 应用中使用 MongoDB的方法
    • 如何使用七牛Python SDK写一个同步脚本及使用教程
    • Pycharm学习教程(6) Pycharm作为Vim编辑器使用
    • python根据给定文件返回文件名和扩展名的方法
    • 搭建Python的Django框架环境并建立和运行第一个App的教程
    • python实现simhash算法实例
    • python中ConfigParse模块的用法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有