標籤:python
1.查詢模組:按目錄依次尋找需要匯入的模組,模組目錄一般在:/usr/lib64/python2.7
In [2]: sys.pathOut[2]:['','/usr/bin','/usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg','/usr/lib64/python27.zip', '/usr/lib64/python2.7','/usr/lib64/python2.7/plat-linux2','/usr/lib64/python2.7/lib-tk','/usr/lib64/python2.7/lib-old','/usr/lib64/python2.7/lib-dynload','/usr/lib64/python2.7/site-packages','/usr/lib/python2.7/site-packages','/usr/lib/python2.7/site-packages/python_memcached-1.58-py2.7.egg','/usr/lib/python2.7/site-packages/IPython/extensions','/root/.ipython']
2.自訂模組目錄
方法一:sys.path.append(),一般加在目錄列表最後
In [3]: sys.path.append("/root/python/")In [4]: sys.pathOut[4]:['','/usr/bin','/usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg','/usr/lib64/python27.zip','/usr/lib64/python2.7','/usr/lib64/python2.7/plat-linux2','/usr/lib64/python2.7/lib-tk','/usr/lib64/python2.7/lib-old','/usr/lib64/python2.7/lib-dynload','/usr/lib64/python2.7/site-packages','/usr/lib/python2.7/site-packages','/usr/lib/python2.7/site-packages/python_memcached-1.58-py2.7.egg','/usr/lib/python2.7/site-packages/IPython/extensions','/root/.ipython', '/root/python/']
方法二:修改環境變數,一般加在目錄列表前面
vim /root/.bashrc # 加入 export PYTHONPATH=/root/pythonsource /root/.bashrc # 重新整理
例子:統計一個檔案,行數、單詞數、字元數(和wc命令相同效果)
說明:為了避免使用split切割之後,最後多出一個Null 字元串,而使用count()
#/usr/bin/env pythondef count(s):char = len(s)words = len(s.split())lines = s.count("\n")print lines,words,charfile1 = open("/etc/passwd","r")s = file1.read()count(s)
3.指令碼形式,匯入模組,指令碼名字不能是數字,會產生一個編譯檔案
例子:
#!/usr/bin/env pythonimport wc
說明:目錄下生產編譯檔案:wc.pyc
4.py和wc.py的__name__內建變數不一樣,前者是wc,或者是__main__,修改wc.py,執行自己時,輸出自己的結果,被調用時,執行不顯示源結果:
wc.py:
#/usr/bin/env pythondef count(s):char = len(s)words = len(s.split())lines = s.count("\n")print lines,words,charif __name__ == "__main__":file1 = open("/etc/passwd","r")s = file1.read()count(s)
test.py:
#!/usr/bin/env pythonimport wcs = open("/root/python/10.py","r").read()wc.count(s)
5.包的形式,匯入模組
四種匯入方法:在包目錄dir下建立一個__init__.py空檔案
方法一:
from dir import wcwc.count("abc")
方法二:
import dir.wcdir.wc.count("abc")
方法三:
from dir.wc import countcount("abc")
方法四:別名
from dir.wc import count as countcount("abc")
6.物件導向編程:python、java、C++;面向過程編程:C、函數式編程、shell
類的(靜態)屬性:(人類的五官,理解為變數)
類的(動態)方法:(人類吃穿住行,理解為一個函數)
對象:類的執行個體化,之後才能有屬性和方法
7.類的建立
類的方法中,至少有一個參數self
調用屬性時,不帶括弧
調用方法時,使用括弧;方法調用屬性時,至少有一個self參數
屬性調用其他方法:類名.屬性名稱
例子:
class People():color = "yellow"def think(self):self.color = "black"print ("My color is %s "% (self.color))ren = People() # 類的執行個體化print ren.color # 類的屬性外部調用ren.think() # 類的方法外部調用,如加上print,則多一個預設return值none
運行結果:
yellow
My color is black
8.私人屬性在定義的類中的內建函式中被調用
例子:
class People():color = "yellow"__age = 27def think(self):self.color = "black"print self.__age # 內建函式調用類的私人屬性,外部函數不能直接調用print ("My color is %s "% (self.color))ren = People()print ren.colorren.think()
9.外部調用私人屬性(格式:執行個體化名._類名屬性名稱),一般只是測試用
例子:
class People():color = "yellow"__age = 27def think(self):self.color = "black"print self.__ageprint ("My color is %s "% (self.color))ren = People()print ren.colorren.think()print ren._People__age # 外部調用私人屬性
10.類的方法
公有方法:內部和外部都可以調用
私人方法:內建函式調用
動態方法:classmethod()函數處理,沒有被調用的類的其他參數不會載入進記憶體中
靜態方法:
方法的定義和函數一樣,但是需要把self作為第一個參數,如果還是有其他參數,繼續加上;類執行個體化之後,採用“類名.方法名()”調用
例子1:私人方法調用
class People():color = "yellow"__age = 27def __think(self):self.color = "black"print self.__ageprint ("My color is %s "% (self.color))def test(self):self.__think() # 類的私人方法調用ren = People()ren.test() # 類的私人方法調用
例子2:動態方法引動過程
class People():color = "yellow"__age = 27def __think(self):self.color = "black"print self.__ageprint ("My color is %s "% (self.color))def test(self):print ("Testing...")cm = classmethod(test) # 動態方法定義ren = People()ren.cm() # 動態方法引動過程
例子3:靜態方法調用:
類函數不帶self參數,該函數使用staticmethod()函數處理(如果不處理,缺少self,,調用時會報錯),載入關於這個類的所有東西
class People():color = "yellow"__age = 27def __think(self):self.color = "black"print self.__ageprint ("My color is %s "% (self.color))def test(): # 內建函式,不帶selfprint ("Testing...")#print People.color # 因為沒有self,不能調用該類的屬性cm = staticmethod(test) # 靜態方法定義ren = People()ren.cm() # 靜態方法調用
例子4:加裝飾器,只對下面的一個函數起作用,就可以使用類的方法調用了
class People():color = "yellow"__age = 27def __think(self):self.color = "black"print self.__ageprint ("My color is %s "% (self.color))@classmethod # 加裝飾器def test(self): # 帶selfprint ("Testing...")@staticmethod # 加裝飾器def test1(): # 不帶selfprint ("Testing1..")ren = People()People.test() # 類的方法調用People.test1() # 類的方法調用
python—執行個體3