W!o 的派生‧十日談之《七》

5141wQKpnDL

cover

Dive Into Python 》 is a free Python book for experienced programmers.

It was originally hosted at DiveIntoPython.org, but the author has pulled down all copies. It is being mirrored here. You can read the book online, or download it in a variety of formats. It is also available in multiple languages.

我們讀書越多,就越發現我們是無知的。── 雪萊

Dive Into Python 3 》 covers Python 3 and its differences from Python 2. Compared to Dive Into Python, it’s about 20% revised and 80% new material. The book is now complete, but feedback is always welcome.

腹有詩書氣自華,讀書萬卷始通神。── 蘇軾

既是活在資訊爆炸的時代,怎麼可能缺乏知識的呢?這事頂不好說,借人家的話來講︰

蒲松齡‧《阿寶

異史氏曰︰『性癡則其志凝,故書癡者文必工,藝癡者技必良。世之落拓而無成者,皆自謂不癡者也。且如粉花蕩產,盧雉頃家,顧癡人事哉!以是知慧黠而過,乃是真癡,彼孫子何癡乎!』

密尔顿‧《论出版自由

因为书籍并不是绝对死的东西。它包藏着一种生命的潜力,和作者一样活跃。不仅如此,它还象一个宝瓶,把创作者活生生的智慧中最纯净的菁华保存起来。我知道它们是非常活跃的,而且繁殖力也是极强的,就象神话中的龙齿①一样。当它们被撒在各处以后,就可能长出武士来。但是,从另一方面来说,如果不特别小心的话,误杀好人和误禁好书就会同样容易。杀人只是杀死了一个理性的动物,破坏了一个上帝的象;而禁止好书则是扼杀了理性本身,破坏了瞳仁中的上帝圣象②。

也許『郭沫若』說的好︰

』是『』的,書是『』的。『』『』讀『』『』,可以把『』讀『』。『』『』讀『』『』,可以把『』讀『』。

 

如何『』 Python ?就從『』本『派生』的『』開始。因『』故及於它『』它『』,以至於『』而已!!

□︰ 難道都不用練習的嗎?

○︰此語未免太癡!《論語》

學而』篇開宗明義講︰『』而時『』之的吧!!

比方說《潜入派生》 Dive into Python 第二章第四節

《 2.4. Everything Is an Object 》講︰

2.4.2. What’s an Object?

Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute __doc__, which returns the doc string defined in the function’s source code. The sys module is an object which has (among other things) an attribute called path. And so forth.

Still, this begs the question. What is an object? Different programming languages define “object” in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser; some objects have neither attributes nor methods (more on this in Chapter 3), and not all objects are subclassable (more on this in Chapter 5). But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function (more in this in Chapter 4).

This is so important that I’m going to repeat it in case you missed it the first few times: everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Even modules are objects.

那麼我們將如何理解『在派生中,一切皆是物件。』這句話呢?要是不深入『派生』的『語法』以及『語意』,只依靠著『語用』根本是不足以『回答』的吧!而且不同的『程式語言』,『口號』不同,『哲學』也不一樣!!Python 號稱『多典範』,『程序』為主的寫作、『物件』中心之編程,『泛函式』概念的淺嚐,……等等,也許可以這麼說︰

『派生』支持的『作法』,讓它『語法』上『方便容易』。

『派生』不支持的『行事』,也可『己選自擇』的『表現』。

因此想要掌握 Python 語言,『深思』、『細慮』以及『多實驗』或許是個好的起步。就像嘗試看看,在『派生』中,什麼是『函式』 function 的呢?

 

pi@raspberrypi ~ $ python3
Python 3.2.3 (default, Mar  1 2013, 11:53:50) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

# 列出『整數』物件有的『屬性』與『方法』
>>> dir(3)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']


# 『泛函式』小試
>>> def 求值(函數,參數):
...     return 函數(參數)
... 

# 『函數』定義
>>> def f(x):
...     return x*x +2*x -1
... 

# 『函數』求值
>>> 求值(f,5)
34

# f 是『函數』物件的『標識符』
>>> f
<function f at 0x76a5cf60>

# 『函數』呼叫,是由『標識符』 f 與『參數元組』 (5) 所構成
>>> f (5)
34

# 『函數』的重要『屬性』是『可呼叫』 callable
>>> callable(f)
True

# 列出『函數』物件有的『屬性』與『方法』
>>> dir(f)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

# λ 『匿名函數』
>>> 求值(lambda x:x*x + 2*x -1,5)
34

>>> lambda x:x*x + 2*x -1
<function <lambda> at 0x76a5ced0>

>>> dir(lambda x:x*x + 2*x -1)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

 

─── 豈是一句『可呼叫』 callable 了得!! ───