国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python內置函數(shù)OCT詳解

2019-11-25 16:29:54
字體:
來源:轉載
供稿:網友

英文文檔:

復制代碼 代碼如下:
oct ( x )
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.

說明:

1. 函數(shù)功能將一個整數(shù)轉換成8進制字符串。如果傳入浮點數(shù)或者字符串均會報錯。

>>> a = oct(10)>>> a'0o12'>>> type(a) # 返回結果類型是字符串<class 'str'>>>> oct(10.0) # 浮點數(shù)不能轉換成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. 如果傳入參數(shù)不是整數(shù),則其必須是一個定義了__index__并返回整數(shù)函數(shù)的類的實例對象。

# 未定義__index__函數(shù),不能轉換>>> 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__函數(shù),但是返回值不是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__函數(shù),而且返回值是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'

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大连市| 华阴市| 浦东新区| 中西区| 高州市| 廉江市| 河间市| 含山县| 牙克石市| 北辰区| 克山县| 右玉县| 玉田县| 高青县| 保定市| 梁河县| 德安县| 通道| 珠海市| 杨浦区| 建始县| 教育| 南投县| 建平县| 弋阳县| 邓州市| 儋州市| 衡山县| 五莲县| 荔波县| 汨罗市| 高平市| 新丰县| 长白| 闸北区| 招远市| 闽侯县| 同江市| 定兴县| 沐川县| 鄄城县|