英文文件:
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. 函數函數將一個整數轉換成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):
>>> a = Student('Kim',10)>>> oct(a)Traceback (most recent call last): in oct(a)TypeError: 'Student' object cannot be interpreted as an integer 函數OCT詳解,更多相關文章請關注PHP中文網(www.php.cn)!