偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

常用Python應(yīng)用技巧內(nèi)容分析

開發(fā) 后端
我們今天在這篇文章中為大家詳細(xì)總結(jié)了幾個(gè)比較常用到的Python應(yīng)用技巧,以方便大家在實(shí)際應(yīng)用中能夠獲得一些幫助。

Python編程語言作為一款功能強(qiáng)大的面向?qū)ο箝_源編程語言,其應(yīng)用特點(diǎn)比較突出,極大的方便了開發(fā)人員的應(yīng)用。在學(xué)習(xí)的過程中,我們可以從相關(guān)的實(shí)踐中去積累經(jīng)驗(yàn)來熟練掌握這一語言的應(yīng)用技巧。比如今天為大家介紹的Python應(yīng)用技巧的相關(guān)內(nèi)容就是一個(gè)比較重要的經(jīng)驗(yàn)。

Python應(yīng)用技巧1. self, cls 不是關(guān)鍵字

在python里面,self, cls 不是關(guān)鍵字,完全可以使用自己寫的任意變量代替實(shí)現(xiàn)一樣的效果

代碼1

  1. class MyTest:   
  2. myname = 'peter'   
  3. def sayhello(hello):   
  4. print "say hello to %s" % hello.myname   
  5. if __name__ == "__main__":   
  6. MyTest().sayhello()   
  7. class MyTest: myname = 'peter' def sayhello(hello): print "say hello 
    to %s" % hello.myname if 
    __name__ == "__main__": MyTest().sayhello() 

代碼1中, 用hello代替掉了self, 得到的是一樣的效果,也可以替換成java中常用的this.

結(jié)論 : self和cls只是python中約定的寫法,本質(zhì)上只是一個(gè)函數(shù)參數(shù)而已,沒有特別含義。

任何對(duì)象調(diào)用方法都會(huì)把把自己作為該方法中的第一個(gè)參數(shù),傳遞到函數(shù)中。(因?yàn)樵趐ython中萬物都是對(duì)象,所以當(dāng)我們使用Class.method()的時(shí)候,實(shí)際上的第一個(gè)參數(shù)是我們約定的cls)

Python應(yīng)用技巧2. 類的定義可以動(dòng)態(tài)修改

代碼2

  1. class MyTest:   
  2. myname = 'peter'   
  3. def sayhello(self):   
  4. print "say hello to %s" % self.myname   
  5. if __name__ == "__main__":   
  6. MyTest.myname = 'hone'   
  7. MyTest.sayhello = lambda self,name: "I want say hello to %s" % name   
  8. MyTest.saygoodbye = lambda self,name: "I do not want say goodbye to %s" % name   
  9. print MyTest().sayhello(MyTest.myname)   
  10. print MyTest().saygoodbye(MyTest.myname)   
  11. class MyTest: myname = 'peter' def sayhello(self): print "say hello to %s" 
    % self.myname if 
    __name__ == "__main__": MyTest.myname = 'hone' MyTest.sayhello 
    lambda self,name: "I want say hello to %s" % name MyTest.saygoodbye = 
    lambda self,name: "I do not want say goodbye to %s" % name print MyTest().
    sayhello(MyTest.myname) print MyTest().saygoodbye(MyTest.myname) 

這里修改了MyTest類中的變量和函數(shù)定義, 實(shí)例化的instance有了不同的行為特征。

Python應(yīng)用技巧3. decorator

decorator是一個(gè)函數(shù), 接收一個(gè)函數(shù)作為參數(shù), 返回值是一個(gè)函數(shù)

代碼3

  1. def enhanced(meth):   
  2. def new(self, y):   
  3. print "I am enhanced"   
  4. return meth(self, y)   
  5. return new   
  6. class C:   
  7. def bar(self, x):   
  8. print "some method says:", x   
  9. bar = enhanced(bar)   
  10. def enhanced(meth): def new(self, y): print "I am enhanced" 
    return meth(self, y) return new class C: def bar(self, x): 
    print "some method says:", x 
    bar = enhanced(bar) 

上面是一個(gè)比較典型的應(yīng)用

以常用的@classmethod為例

正常的使用方法是

代碼4

  1. class C:   
  2. @classmethod   
  3. def foo(cls, y):   
  4. print "classmethod", cls, y   
  5. class C: @classmethod def foo(cls, y): print "classmethod", cls, y 

以上就是我們?yōu)榇蠹医榻B的有關(guān)Python應(yīng)用技巧的相關(guān)內(nèi)容。

【編輯推薦】

  1. 安裝Python簡(jiǎn)單操作方法分享
  2. Python Helloworld程序簡(jiǎn)單實(shí)現(xiàn)
  3. Python Class正確應(yīng)用代碼示例剖析
  4. 調(diào)用Python腳本基本應(yīng)用方式簡(jiǎn)述
  5. Python同步隊(duì)列正確應(yīng)用方式解析
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-02-03 17:10:12

Python編寫

2010-01-04 10:34:01

Silverlight

2010-01-28 15:09:36

C++資源管理

2011-05-26 17:19:05

中間件

2010-03-03 15:39:50

Python抓取網(wǎng)頁(yè)內(nèi)

2010-01-27 18:19:13

Android畫圖

2010-02-06 13:52:39

C++ profile

2010-01-11 14:59:01

TP-Link交換機(jī)配

2010-02-26 10:46:12

WCF行為擴(kuò)展

2016-10-21 14:35:52

Pythonwebget方法

2016-10-20 20:21:09

Python爬蟲技巧

2010-02-05 18:04:21

C++剪切板

2010-02-22 13:28:05

WCF異步調(diào)用

2010-02-22 17:51:46

WCF傳較大數(shù)據(jù)

2020-10-20 14:10:51

Python代碼字符串

2010-03-03 13:12:56

Python圖像處理

2020-02-23 23:29:07

Python編程開發(fā)

2024-02-07 12:32:00

重構(gòu)技巧PythonCounter

2010-07-26 09:22:05

Perl命令行

2010-03-01 18:11:40

WCF數(shù)據(jù)契約變更
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)