【head first python】2.共用你的代碼 函數模組

來源:互聯網
上載者:User

標籤:des   9.png   coding   構建   utils   graham   org   div   movies   

#coding:utf-8#注釋代碼!#添加兩個注釋,一個描述模組,一個描述函數‘‘‘這是nester.py模組,提供了一個名為print_lol()的函數,這個函數的作用是列印列表,其中可能包含(也可能不包含)嵌套列表。‘‘‘def print_lol(the_list):    ‘‘‘這個函數取一個位置參數,名為the_list,這可以是任何python列表    (也可以是包含嵌套列表的列表),所指定列表的每個資料項目    回(遞迴地)輸出到螢幕上,每個資料項目各佔一行。‘‘‘    for item in the_list:        if isinstance(item,list):            print_lol(item)        else:            print itemprint_lol(movies)

 

模組就是一個包含python代碼的文字檔,以.py結尾。

 

#試著使用movies = movies = [‘The Holy Grail‘, 1975, ‘The Life of Brain‘, 1979,          ["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]]print_lol(movies)

 

【準備發布】

1.為模組建立一個檔案夾nester

2.在檔案夾中創造一個setup.py檔案

#coding:utf-8from distutils.core import setup#從python發布工具中匯入setup函數#這些是setup函數的參數setup(    name                    =‘nester‘,    version                    =‘1.0.0‘,    py_moudles                =[‘nester‘],    author                    =‘HETAO‘,    author_email            =‘[email protected]‘,    url                        =‘https://home.cnblogs.com/u/Archimedes/‘,    description                =‘A simple printer of nested lists‘,    )

 

構建發布

1.構建一個發布檔案

2.將發布安裝到python本機複本中

 

 

 這時nester檔案夾中會出現兩個新的檔案夾,他們是由發布工具建立的

【匯入模組並使用】

Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import nester
>>>

 

 可以看到匯入成功了,接下來試著使用它

Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> import nester>>> movies = movies = [‘The Holy Grail‘, 1975, ‘The Life of Brain‘, 1979,          ["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]]>>> print_lol(movies)Traceback (most recent call last):  File "<pyshell#2>", line 1, in <module>    print_lol(movies)NameError: name ‘print_lol‘ is not defined  #python沒有找到你的函數!!!>>> 

 

這是我們就要認識到一個東西叫做【模組的命名空間】

命名空間i就像是姓氏,正確的使用方式是nester.print_lo(movies)

>>> nester.print_lol(movies)The Holy Grail1975The Life of Brain1979Graham ChapmanMichael PalinJohn CleeseTerry GilliamEric Idle>>> 

 

 這樣就好了

註:如果使用from nester import print_lol 就可以把指定函數增加到當前命名空間,也就是說不用使用nester這個命名空間限定了。

但這種做法並不被提倡,因為在這時候要注意如果當前命名空間中已經定義了一個同名的函數print_lol,那麼之前這個函數就會被引入的函數所覆蓋而帶來麻煩。

【註冊PYPI網站】

要在PYPI上發布,就要先註冊一個帳號(https://pypi.python.org)

 

截止 【【P49】】

【head first python】2.共用你的代碼 函數模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.