Python內(nèi)置函數(shù)OCT詳解
英文文檔:
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.
說(shuō)明:
1. 函數(shù)功能將一個(gè)整數(shù)轉(zhuǎn)換成8進(jìn)制字符串。如果傳入浮點(diǎn)數(shù)或者字符串均會(huì)報(bào)錯(cuò)。
>>> a = oct(10)
>>> a
'0o12'
>>> type(a) # 返回結(jié)果類型是字符串
<class 'str'>
>>> oct(10.0) # 浮點(diǎn)數(shù)不能轉(zhuǎn)換成8進(jìn)制
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') # 字符串不能轉(zhuǎn)換成8進(jìn)制
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
oct('10')
TypeError: 'str' object cannot be interpreted as an integer
2. 如果傳入?yún)?shù)不是整數(shù),則其必須是一個(gè)定義了__index__并返回整數(shù)函數(shù)的類的實(shí)例對(duì)象。
# 未定義__index__函數(shù),不能轉(zhuǎn)換
>>> 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類型,不能轉(zhuǎn)換
>>> 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類型,能轉(zhuǎn)換
>>> 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中的內(nèi)置函數(shù)getattr()介紹及示例
- Python內(nèi)置函數(shù)bin() oct()等實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換
- Python常用內(nèi)置函數(shù)總結(jié)
- Python內(nèi)置函數(shù)dir詳解
- Python內(nèi)置函數(shù)Type()函數(shù)一個(gè)有趣的用法
- Python內(nèi)置函數(shù)的用法實(shí)例教程
- Python標(biāo)準(zhǔn)庫(kù)內(nèi)置函數(shù)complex介紹
- Python入門及進(jìn)階筆記 Python 內(nèi)置函數(shù)小結(jié)
相關(guān)文章
python解決pandas處理缺失值為空字符串的問(wèn)題
下面小編就為大家分享一篇python解決pandas處理缺失值為空字符串的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
python語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了python語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
在Ubuntu中安裝并配置Pycharm教程的實(shí)現(xiàn)方法
這篇文章主要介紹了在Ubuntu中安裝并配置Pycharm教程的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
解決新django中的path不能使用正則表達(dá)式的問(wèn)題
今天小編就為大家分享一篇解決新django中的path不能使用正則表達(dá)式的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
解決TensorFlow GPU版出現(xiàn)OOM錯(cuò)誤的問(wèn)題
今天小編就為大家分享一篇解決TensorFlow GPU版出現(xiàn)OOM錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
python網(wǎng)絡(luò)爬蟲之協(xié)程的實(shí)現(xiàn)方法
這篇文章主要介紹了python網(wǎng)絡(luò)爬蟲之協(xié)程的實(shí)現(xiàn)方法,協(xié)程Coroutine又稱微線程,是一種用戶態(tài)內(nèi)的上下文切換技術(shù),簡(jiǎn)而言之,就是通過(guò)一個(gè)線程實(shí)現(xiàn)代碼塊相互切換執(zhí)行,需要的朋友可以參考下2023-08-08
Python編程實(shí)現(xiàn)生成特定范圍內(nèi)不重復(fù)多個(gè)隨機(jī)數(shù)的2種方法
這篇文章主要介紹了Python編程實(shí)現(xiàn)生成特定范圍內(nèi)不重復(fù)多個(gè)隨機(jī)數(shù)的2種方法,涉及Python基于random生成隨機(jī)數(shù)的常見操作技巧,需要的朋友可以參考下2017-04-04
python爬蟲框架talonspider簡(jiǎn)單介紹
本文給大家介紹的是使用python開發(fā)的爬蟲框架talonspider的簡(jiǎn)單介紹以及使用方法,有需要的小伙伴可以參考下2017-06-06
利用python爬取散文網(wǎng)的文章實(shí)例教程
這篇文章主要跟大家介紹了利用python爬取散文網(wǎng)文章的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06

