bin(x)
英文說明:Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
New in version 2.6.
中文說明:將整數x轉換為二進制字符串,如果x不為Python中int類型,x必須包含方法__index__()并且返回值為integer;
參數x:整數或者包含__index__()方法切返回值為integer的類型;
版本:bin函數是python2.6中新增函數,使用時要注意版本問題。
實例講解:
#整數的情況 >>> bin(521) #這里的顯示結果形式與我們平時習慣有些差別,主要是前面多了0b,這是表示二進制的意思。 '0b1000001001' #非整型的情況,必須包含__index__()方法切返回值為integer的類型 >>> class myType: ... def __index__(self): ... return 35 >>> myvar = myType() >>> bin(myvar) '0b1000001001'
PS:改函數非常簡單,但是要注意版本,和參數類型。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com