十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
這篇文章將為大家詳細(xì)講解有關(guān)Python中提高代碼效率技巧是怎么樣的,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
成都創(chuàng)新互聯(lián)主營(yíng)開化網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,App定制開發(fā),開化h5成都小程序開發(fā)搭建,開化網(wǎng)站營(yíng)銷推廣歡迎開化等地區(qū)企業(yè)咨詢
對(duì)象相等。您認(rèn)為type(a) == type(b)和type(a) is type(b)之間的不同是什么?為什么會(huì)選擇后者?函數(shù)isinstance()與這有什么關(guān)系?
在例4.1 中我們提供了一段腳本來(lái)演示在運(yùn)行時(shí)環(huán)境使用isinstance() 和 type()函數(shù)。隨后我們討論type()的使用以及怎么將這個(gè)例子移植為改用 isinstance()。運(yùn)行 typechk.py, 我們會(huì)得到以下輸出:
-69 is a number of type: int 9999999999999999999999 is a number of type: long 98.6 is a number of type: float (-5.2+1.9j) is a number of type: complex xxx is not a number at all!!
例4.1 檢查類型(typechk.py)函數(shù)displayNumType() 接受一個(gè)數(shù)值參數(shù),Python技巧中使用內(nèi)建函數(shù)type()來(lái)確認(rèn)數(shù)值的類型(或不是一個(gè)數(shù)值類型)。
#!/usr/bin/env python def displayNumType(num): print num, 'is', if isinstance(num, (int, long, float, complex)): print 'a number of type:', type(num).__name__ else: print 'not a number at all!!' displayNumType(-69) displayNumType(9999999999999999999999L) displayNumType(98.6) displayNumType(-5.2+1.9j) displayNumType('xxx')
關(guān)于Python中提高代碼效率技巧是怎么樣的就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。