Python關於型別的一些內建函式

2021-07-22 16:41:51 字數 1026 閱讀 7114

str():返回標準型別

type():返回任意型別

isinstance(object, classinfo)

如果引數object是classinfo的例項,或者object是classinfo類的子類的乙個例項, 返回true。如果object不是乙個給定型別的的物件, 則返回結果總是false。

如果classinfo不表示乙個類(型別物件), 那麼它要麼是乙個類的元組, 或者遞迴地包含這樣的(由資料型別構成的)元組.其他的序列型別是不被允許的。

如果classinfo不是一種資料型別或者由資料型別構成的元組,將引發乙個typeerror異常。

在python的idle中或命令列直譯器中鍵入help(isinstance)即可獲得該函式的幫助資訊:

isinstance(object, class-or-type-or-tuple)

檢查型別--通過type()確認數值型別

>>> def display(num):

print num,'is',

if isinstance(num,(int,long,float,complex)):

print 'a number of type:',type(num).__name__

else:

print 'not a number'

>>> display(-69)

-69 is a number of type: int

>>> display(999999999999l)

999999999999 is a number of type: long

>>> display(98.6)

98.6 is a number of type: float

>>> display(-5.2+1.9j)

(-5.2+1.9j) is a number of type: complex

>>> display('***')

*** is not a number

>>>

Python一些內建函式

dir obj 顯示物件的屬性,如果沒有提供引數,則顯示全域性變數的名字 help obj 顯示物件的文件字串,如果沒有提供任何引數,進入互動式幫助 len obj 返回物件長度 open fn,mode 以mode方式開啟乙個檔名為fn的檔案 range start,stop step 返回乙個整...

Python一些內建函式

來判斷乙個物件是否是乙個已知的型別。語法 isinstance object,classinfo bool 引數 返回值 isinstance 與 type 區別 示例 classa pass class b a pass isinstance a a true type a a true isin...

python的一些內建函式

python並非我的第一語言,所以之前看python 的時候遇到過一些內建函式的時候,總是以物件導向不看細節的心情大概理解用法之後就置之不理了。但是內建函式實在太短小精悍,很好用,所以總是不可避免的要遇到,所以還是下決心好好分析一下。我現在遇到過的有幾種 filter,map,reduce,lamb...