1.python中類方法、類實(shí)例方法、靜態(tài)方法有何區(qū)別?
區(qū)別:
類方法和靜態(tài)方法都可以被類和類實(shí)例調(diào)用,類實(shí)例方法僅可以被類實(shí)例調(diào)用類方法的隱含調(diào)用參數(shù)是類,而類實(shí)例方法的隱含調(diào)用參數(shù)是類的實(shí)例,靜態(tài)方法沒有隱含調(diào)用參數(shù)使用示例:
python代碼:
1 class A(object): 2 def foo(self,x): 3 #類實(shí)例方法 4 PRint "executing foo(%s,%s)"%(self,x) 5 6 @classmethod 7 def class_foo(cls,x): 8 #類方法 9 print "executing class_foo(%s,%s)"%(cls,x) 10 11 @staticmethod 12 def static_foo(x): 13 #靜態(tài)方法 14 print "executing static_foo(%s)"%x調(diào)用方法
1 a = A() 2 a.foo(1) //print : executing foo(<__main__.A object at 0xb77d67ec>,1)3 4 a.class_foo(1) //executing class_foo(<class '__main__.A'>,1) 5 A.class_foo(1) //executing class_foo(<class '__main__.A'>,1) 6 7 a.static_foo(1) //executing static_foo(1) 8 A.static_foo(1) //executing static_foo(1)2.python中xrange和range的異同
xrange的用法與range完全相同,所不同的是xrange生成的不是一個(gè)list,而是一個(gè)生成器。要生成很大的數(shù)字序列的時(shí)候,用xrange會(huì)比range性能優(yōu)很多,因?yàn)椴恍枰簧蟻砭烷_辟一塊很大的內(nèi)存空間。
range會(huì)直接生成一個(gè)list對(duì)象:
1 >>> a = range(0, 50)2 >>> print type(a)3 <type 'list'>4 >>> print a5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]而xrange則不會(huì)直接生成一個(gè)list,而是每次調(diào)用返回其中的一個(gè)值:
1 >>> a = xrange(0, 50)2 >>> print type(a)3 <type 'xrange'>4 >>> print a5 xrange(50)3.請(qǐng)用python實(shí)現(xiàn)如下的C代碼問號(hào)表達(dá)式:
int n = a>b ?(a-b):0
要求使用最簡(jiǎn)單的方式實(shí)現(xiàn)。
1 >>> n=(3>4) and (3-4) or 02 >>> print n3 04 >>> n=(3<4) and (3-4) or 05 >>> print n6 -14.python的多線程的實(shí)現(xiàn)機(jī)制是什么?在什么情況下使用多線程能明顯提高程序效率?
5.寫出正則表達(dá)式從一個(gè)字符串中提取鏈接地址,如以下字符串<a href=http://www.mianwww.com/html/category/it-interview/flex>Flex</a>
需要提取的鏈接為“http://www.mianwww.com/html/category/it-interview/flex”
1 >>> href = re.findall(r"<a.*?href=(http://.*?)>", string)2 >>> print href3 ['http://www.mianwww.com/html/category/it-interview/flex']有人說后面.*?中的.和?不要,我試了下不行
1 >>> href = re.findall(r"<a.*?href=(http://*)>", string)2 >>> print href3 []4 >>> href = re.findall(r"<a.*?href=(http://*?)>", string)5 >>> print href6 []6.反轉(zhuǎn)由單詞和不定個(gè)數(shù)空格組成的字符串,要求單詞中的字母順序不變。如:"I am a boy"反轉(zhuǎn)成“boy a am I”。
1 >>> import re2 >>> string = "I am a boy"3 >>> revWords = ''.join(re.split(r'(/s+)', string)[::-1])4 >>> print revwords5 boy a am I未完待續(xù)。。。
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注