python基礎學習筆記(十二)
2013-05-16 21:47 ?蟲師 閱讀( ... ) 評論( ... ) 編輯 收藏?
模塊
前面有簡單介紹如何使用import從外部模塊獲取函數并且為自己的程序所用:
>>>
import
math
>>> math.sin(0)
#
sin為正弦函數
0.0
?
模塊是程序
任何 python 程序都可以作為模塊導入。假設寫如下程序,并且將它保存為以 C:\python\hello.py
#
hello.py
print
"
hello,world!
"
下面通過 python 解釋器調用:
>>>
import
sys
>>> sys.path.append(
'
c:/python
'
)
>>>
import
hello
hello,world!
再來一次:
>>>
import
hello
>>>
怎么這次沒結果?因為導入模塊并不意味著在導入進執行某些操作。它們主要用于定義,比如變量、函數和類等。此外,因為只需要定義這些東西一次,導入模塊多次和導入一次的效果是一樣的。
?
?
模塊用于定義
1、在模塊中定義函數
假設我們編寫了一個類似代碼的模塊,將其保存為 hello2.py? 文件。
#
hello2.py
def
hello():
print
"
hello, world !
"
保存后,可以像下面這樣導入:
>>>?import?hello2
模塊會被執行,這意味著hello 函數在模塊的作用被定義了。因此可以通過以下方式來訪問函數:
>>>?hello2.hello()
hello.world!
?
?
2、在模塊中增加測試代碼
模塊用來定義函數、類和其他內容,有時候在模塊中添加一些檢查模塊本身是否正常工作的測試代碼是非常有用的。?
#
hello3.py
def
hello():
print
"
hello.world!
"
def
test():
hello()
if
__name__
==
'
__main__
'
: test()
f?__name__?==?'__nain__'?解釋 :
python 文件的后綴為 .py? , .py 文件可以用來直接運行,就像一個獨立的小程序;也可以用來作為模塊被其它程序調用。
__name__ 是模塊的內置屬性,如果等于 '__main__'?側表示直接被使用,那么將執行方法 test() 方法;如果是被調用則不執行? if? 判斷后面的 test() 方法。
執行結果:
>>>
import
hello3
#
表示hello3模塊被調用,不執行test方法,所以沒有輸出
>>> hello3.hello()
#
表示程序執行,調用test方法
hello.world!
>>> hello3.hello()
#
這里是不是可以反復調用test方法
hello.world!
?
?
?
讓模塊可用
前面的例子中,我們改變了 sys.path ,其中包含了一個目錄列表,解釋器在該列表中查找模塊。在理想情況下,一開始 sys.path 本身就應該包含正確的目錄,有兩方法可以做到這一點:?一是將模塊放置在合適的位置,別外一種是告訴解釋器去哪里查找需要的模塊。
1、將模塊放置在正確的位置
來看看 python 解釋器會從哪里查找模塊
>>>
import
sys,pprint
>>>
pprint.pprint(sys.path)
[
''
,
'
I:\\Python27\\Lib\\idlelib
'
,
'
C:\\Windows\\system32\\python27.zip
'
,
'
I:\\Python27\\DLLs
'
,
'
I:\\Python27\\lib
'
,
'
I:\\Python27\\lib\\plat-win
'
,
'
I:\\Python27\\lib\\lib-tk
'
,
'
I:\\Python27
'
,
'
I:\\Python27\\lib\\site-packages
'
,
'
c:/python
'
]
盡管這些目錄下都可以被找到,但site-packages?目錄是最佳選擇。
?
?
2、告訴編譯器去哪里找
以下情況是告訴編譯器去哪兒找的原因:
**? 不希望將自己的模塊填滿 python 解釋器的目錄
**? 沒有在 python 解釋器目錄中存儲文件的權限
**? 想將模塊放到其它位置
編輯 sys.path?, 前面就已經使用了
>>>
import
sys
>>> sys.path.append(
'
c:/python
'
)
但更優雅的做法是配置 pythonpath 環境變量,方法和配置 java? 環境變量類似。
?
?
文檔
模塊信息的自然來源是文檔,除了通過 python 書籍或標準 python 文檔來查看某個函數的含義,也可以通過下面方式: ?
>>>
print
range.
__doc__
range([start,] stop[, step])
->
list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i
+1, i+2, ..., j-1
]; start (!) defaults to 0.
When step
is
given, it specifies the increment (
or
decrement).
For example, range(
4) returns [0, 1, 2, 3]. The end point
is
omitted!
These are exactly the valid indices
for
a list of 4 elements.
這樣就獲得了關于 range 函數的精確描述。
?
?
time? 模塊
time 模塊所包括的函數能夠實現?以下功能:獲得當前時間,操作時間和日期,從字符串讀取時間以及格式化時間為字符串。
time 模塊中重要的函數
?time.asctime 將當前時間格式化為字符串:
>>>
time.asctime()
'
Thu May 16 00:00:08 2013
'
?
?
?
random 模塊
random 模塊包括返回隨機的函數,可以用于模擬或者用于任何產生隨機輸出的程序。
random 模塊中的一些重要函數:
?
下面介紹使用 random 模塊的例子,還需要用到 time 模塊中的函數。
例1: 首先獲得代表時間間隔( 2013 年)限制的實數,這可以通過時間元組的方式來表示日期(使用? -1 表示一周的某天,一年中某天和夏令時,以例讓 python 自己計算),并且對這些元組調用 mktime? :
from
random
import
*
from
time
import
*
data1
= (2013 ,1,1,0,0,0,-1,-1,-1
)
time1
=
mktime(data1)
data2
= (2014 ,1,1,0,0,0,-1,-1,-1
)
time2
=
mktime(data2)
#
然后在這個范圍內生成隨機數
>>> random_time =
uniform(time1,time2)
#
可以將數字轉換成易讀的日期形式
>>>
print
asctime(localtime(random_time))
Fri Jan
18 18:23:16 2013
?
例2: 下面一個例子,假設三個人打牌,首先要保證 54 張牌沒有重復的,第人發手里18張(斗地主就不能平均分配了)。
>>> values = range(1,13) +
'
dwang xwang
'
.split()
#
定義13個數字與大小王
>>> suits =
'
hei hong mei fang
'
.split()
#
定義牌的四種類型(黑、紅、梅、方)
>>> deck = [
'
%s of %s
'
%(v ,s )
for
v
in
values
for
s
in
suits]
#
循環嵌套將其循環組合
>>>
from
pprint
import
pprint
#
調用pprint 模塊
>>> pprint (deck [:18])
#
輸出18張牌
[
'
1 of hei
'
,
'
1 of hong
'
,
'
1 of mei
'
,
'
1 of fang
'
,
'
2 of hei
'
,
'
2 of hong
'
,
'
2 of mei
'
,
'
2 of fang
'
,
'
3 of hei
'
,
'
3 of hong
'
,
'
3 of mei
'
,
'
3 of fang
'
,
'
4 of hei
'
,
'
4 of hong
'
,
'
4 of mei
'
,
'
4 of fang
'
,
'
5 of hei
'
,
'
5 of hong
'
]
#
顯然上面的輸出太整齊,調用隨機函數,隨機一點
>>>
from
random
import
shuffle
>>>
shuffle(deck)
>>> pprint(deck[:18
])
[
'
5 of fang
'
,
'
6 of hong
'
,
'
5 of mei
'
,
'
dwang of fang
'
,
'
xwang of fang
'
,
'
10 of hong
'
,
'
7 of mei
'
,
'
12 of hong
'
,
'
6 of hei
'
,
'
12 of hei
'
,
'
7 of hei
'
,
'
8 of hei
'
,
'
4 of fang
'
,
'
dwang of hei
'
,
'
11 of hei
'
,
'
12 of fang
'
,
'
5 of hei
'
,
'
2 of hong
'
]
不過,依然是有問題的,大小王不應該分類型(黑、紅、梅、方),顯然上面的結果不夠完美。
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

