在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ 標(biāo)準(zhǔn)庫 (1)
標(biāo)準(zhǔn)庫 (4)
如何成為 Python 高手
標(biāo)準(zhǔn)庫 (6)
標(biāo)準(zhǔn)庫 (3)
類(2)
Pandas 使用 (2)
xml
用 tornado 做網(wǎng)站 (5)
文件(1)
練習(xí)
列表(3)
從小工到專家
除法
錯(cuò)誤和異常 (2)
函數(shù)(1)
用 tornado 做網(wǎng)站 (7)
為做網(wǎng)站而準(zhǔn)備
函數(shù)練習(xí)
標(biāo)準(zhǔn)庫 (8)
Pandas 使用 (1)
回顧 list 和 str
字典(1)
用 tornado 做網(wǎng)站 (3)
字符串(1)
函數(shù)(2)
寫一個(gè)簡(jiǎn)單的程序
將數(shù)據(jù)存入文件
語句(5)
SQLite 數(shù)據(jù)庫
集成開發(fā)環(huán)境(IDE)
集合(1)
類(1)
用 tornado 做網(wǎng)站 (6)
用 tornado 做網(wǎng)站 (2)
自省
語句(4)
錯(cuò)誤和異常 (1)
用 tornado 做網(wǎng)站 (4)
集合(2)
列表(1)
標(biāo)準(zhǔn)庫 (1)
生成器
mysql 數(shù)據(jù)庫 (1)
第三方庫
實(shí)戰(zhàn)
運(yùn)算符
類(3)
字典(2)
語句(1)
數(shù)和四則運(yùn)算
語句(2)
文件(2)
MySQL 數(shù)據(jù)庫 (2)
電子表格
迭代器
mongodb 數(shù)據(jù)庫 (1)
特殊方法 (2)
特殊方法 (1)
字符編碼
編寫模塊
用 tornado 做網(wǎng)站 (1)
標(biāo)準(zhǔn)庫 (5)
函數(shù)(4)
類(5)
字符串(2)
關(guān)于 Python 的故事
函數(shù)(3)
字符串(4)
處理股票數(shù)據(jù)
常用數(shù)學(xué)函數(shù)和運(yùn)算優(yōu)先級(jí)
字符串(3)
為計(jì)算做準(zhǔn)備
多態(tài)和封裝
類(4)
迭代
語句(3)
錯(cuò)誤和異常 (3)
分析 Hello
Python 安裝
標(biāo)準(zhǔn)庫 (2)
列表(2)
元組

標(biāo)準(zhǔn)庫 (1)

“Python 自帶‘電池’”,聽說過這種說法嗎?

在 Python 被安裝的時(shí)候,就有不少模塊也隨著安裝到本地的計(jì)算機(jī)上了。這些東西就如同“能源”、“電力”一樣,讓 Python 擁有了無限生機(jī),能夠非常輕而易舉地免費(fèi)使用很多模塊。所以,稱之為“自帶電池”。

它們被稱為“標(biāo)準(zhǔn)庫”。

熟悉標(biāo)準(zhǔn)庫,是進(jìn)行編程的必須。

引用的方式

不僅使標(biāo)準(zhǔn)庫的模塊,所有模塊都服從下述引用方式。

最基本的、也是最常用的,還是可讀性非常好的:

import modulename

例如:

>>> import pprint
>>> a = {"lang":"Python", "book":"www.itdiffer.com", "teacher":"qiwsir", "goal":"from beginner to master"}
>>> pprint.pprint(a)
{'book': 'www.itdiffer.com',
 'goal': 'from beginner to master',
 'lang': 'python',
 'teacher': 'qiwsir'}

在對(duì)模塊進(jìn)行說明的過程中,我以標(biāo)準(zhǔn)庫 pprint 為例。以 pprint.pprint() 的方式應(yīng)用了一種方法,這種方法能夠讓 dict 格式化輸出??纯唇Y(jié)果,是不是比原來更容易閱讀了你?

在 import 后面,理論上可以跟好多模塊名稱。但是在實(shí)踐中,我還是建議大家一次一個(gè)名稱吧。這樣簡(jiǎn)單明了,容易閱讀。

這是用 import pprint 樣式引入模塊,并以 . 點(diǎn)號(hào)的形式引用其方法。

還可以:

>>> from pprint import pprint

意思是從 pprint 模塊中之將 pprint() 引入,然后就可以這樣來應(yīng)用它:

>>> pprint(a)
{'book': 'www.itdiffer.com',
 'goal': 'from beginner to master',
 'lang': 'Python',
 'teacher': 'qiwsir'}

再懶惰一些,可以:

>>> from pprint import *

這就將 pprint 模塊中的一切都引入了,于是可以像上面那樣直接使用每個(gè)函數(shù)。但是,這樣造成的結(jié)果是可讀性不是很好,并且,有用沒用的都拿過來,是不是太貪婪了?貪婪的結(jié)果是內(nèi)存就消耗了不少。所以,這種方法,可以用于常用并且模塊屬性或方法不是很多的情況。

誠然,如果很明確使用那幾個(gè),那么使用類似 from modulename import name1, name2, name3...也未嘗不可。一再提醒的是不能因?yàn)橐肓四K東西而降低了可讀性,讓別人不知道呈現(xiàn)在眼前的方法是從何而來。如果這樣,就要慎用這種方法。

有時(shí)候引入的模塊或者方法名稱有點(diǎn)長,可以給它重命名。如:

>>> import pprint as pr
>>> pr.pprint(a)
{'book': 'www.itdiffer.com',
 'goal': 'from beginner to master',
 'lang': 'python',
 'teacher': 'qiwsir'}

當(dāng)然,還可以這樣:

>>> from pprint import pprint as pt
>>> pt(a)
{'book': 'www.itdiffer.com',
 'goal': 'from beginner to master',
 'lang': 'python',
 'teacher': 'qiwsir'}

但是不管怎么樣,一定要讓人看懂,過了若干時(shí)間,自己也還能看懂。記住:“軟件很多時(shí)候是給人看的,只是偶爾讓機(jī)器執(zhí)行”。

深入探究

繼續(xù)以 pprint 為例,深入研究:

>>> import pprint
>>> dir(pprint)
['PrettyPrinter', '_StringIO', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_commajoin', '_id', '_len', '_perfcheck', '_recursion', '_safe_repr', '_sorted', '_sys', '_type', 'isreadable', 'isrecursive', 'pformat', 'pprint', 'saferepr', 'warnings']

對(duì) dir() 并不陌生。從結(jié)果中可以看到 pprint 的屬性和方法。其中有不少是雙劃線、電話線開頭的。為了不影響我們的視覺,先把它們?nèi)サ簟?/p>

>>> [ m for m in dir(pprint) if not m.startswith('_') ]
['PrettyPrinter', 'isreadable', 'isrecursive', 'pformat', 'pprint', 'saferepr', 'warnings']

對(duì)這幾個(gè),為了能夠搞清楚它們的含義,可以使用 help(),比如:

>>> help(isreadable)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'isreadable' is not defined

這樣做是錯(cuò)誤的。知道錯(cuò)在何處嗎?

>>> help(pprint.isreadable)

別忘記了,我前面是用 import pprint 方式引入模塊的。

Help on function isreadable in module pprint:

isreadable(object)
    Determine if saferepr(object) is readable by eval().

通過幫助信息,能夠查看到該方法的詳細(xì)說明。可以用這種方法一個(gè)一個(gè)地查過來,反正也不多,對(duì)每個(gè)方法都熟悉一些。

注意的是 pprint.PrettyPrinter 是一個(gè)類,后面的是函數(shù)(方法)。

在回頭看看 dir(pprint) 的結(jié)果,關(guān)注一個(gè):

>>> pprint.__all__
['pprint', 'pformat', 'isreadable', 'isrecursive', 'saferepr', 'PrettyPrinter']

這個(gè)結(jié)果是不是眼熟?除了"warnings",跟前面通過列表解析式得到的結(jié)果一樣。

其實(shí),當(dāng)我們使用 from pprint import *的時(shí)候,就是將__all__里面的方法引入,如果沒有這個(gè),就會(huì)將其它所有屬性、方法等引入,包括那些以雙劃線或者單劃線開頭的變量、函數(shù),這些東西事實(shí)上很少被在引入模塊時(shí)使用。

幫助、文檔和源碼

不知道讀者是否能夠記住看過的上述內(nèi)容?反正我記不住。所以,我非常喜歡使用 dir() 和 help(),這也是本教程從開始到現(xiàn)在,乃至到以后,總在提倡的方式。

>>> print pprint.__doc__
Support to pretty-print lists, tuples, & dictionaries recursively.

Very simple, but useful, especially in debugging data structures.

Classes
-------

PrettyPrinter()
    Handle pretty-printing operations onto a stream using a configured
    set of formatting parameters.

Functions
---------

pformat()
    Format a Python object into a pretty-printed representation.

pprint()
    Pretty-print a Python object to a stream [default is sys.stdout].

saferepr()
    Generate a 'standard' repr()-like value, but protect against recursive
    data structures.

pprint.__doc__是查看整個(gè)類的文檔,還知道整個(gè)文檔是寫在什么地方的嗎?

關(guān)于文檔的問題,曾經(jīng)在《類(5)》、《自省》中有介紹。但是,現(xiàn)在出現(xiàn)的是模塊文檔。

還是使用 pm.py 那個(gè)文件,增加如下內(nèi)容:

#!/usr/bin/env Python
# coding=utf-8

"""                                          #增加的
This is a document of the python module.     #增加的
"""                                          #增加的

def lang():
    ...                                      #省略了,后面的也省略了

在這個(gè)文件的開始部分,所有類和方法、以及 import 之前,寫一個(gè)用三個(gè)引號(hào)包括的字符串。那就是文檔。

>>> import sys
>>> sys.path.append("~/Documents/VBS/StarterLearningPython/2code")
>>> import pm
>>> print pm.__doc__

This is a document of the python module.

這就是撰寫模塊文檔的方法,即在 .py 文件的最開始寫相應(yīng)的內(nèi)容。這個(gè)要求應(yīng)該成為開發(fā)習(xí)慣。

Python 的模塊,不僅可以看幫助信息和文檔,還能夠查看源碼,因?yàn)樗情_放的。

還是回頭到 dir(pprint) 中找一找,有一個(gè)__file__,它就告訴我們這個(gè)模塊的位置:

>>> print pprint.__file__
/usr/lib/python2.7/pprint.pyc

我是在 ubuntu 中為例,讀者要注意觀察自己的操作系統(tǒng)結(jié)果。

雖然是 .pyc 文件,但是不用擔(dān)心,根據(jù)現(xiàn)實(shí)的目錄,找到相應(yīng)的 .py 文件即可。

$ ls /usr/lib/python2.7/pp*
/usr/lib/python2.7/pprint.py  /usr/lib/python2.7/pprint.pyc

果然有一個(gè) pprint.py。打開它,就看到源碼了。

$ cat /usr/lib/python2.7/pprint.py

...

"""Support to pretty-print lists, tuples, & dictionaries recursively.

Very simple, but useful, especially in debugging data structures.

Classes
-------

PrettyPrinter()
    Handle pretty-printing operations onto a stream using a configured
    set of formatting parameters.

Functions
---------

pformat()
    Format a Python object into a pretty-printed representation.

....
"""

我只查抄了文檔中的部分信息,是不是跟前面通過__doc__查看的結(jié)果一樣一樣的呢?

請(qǐng)讀者在閑暇時(shí)間,閱讀以下源碼。事實(shí)證明,這種標(biāo)準(zhǔn)庫中的源碼是質(zhì)量最好的。


總目錄   |   上節(jié):編寫模塊   |   下節(jié):標(biāo)準(zhǔn)庫(2)

如果你認(rèn)為有必要打賞我,請(qǐng)通過支付寶:qiwsir@126.com,不勝感激。