M♪o 之學習筆記本《卯》基件︰【䷡】羝羊觸藩

派生碼訊

卯 兔

大壯:利貞。

彖曰:大壯,大者壯也。 剛以動,故壯。 大壯利貞﹔大者正也。正大而天地之情可見矣!

象曰:雷在天上,大壯﹔君子以非禮勿履。

雷 雷鳴在天,聲聞百里。何故『喪羊于易』?《 易 》易曰︰

上六:羝羊觸藩,不能退,不能遂,無攸利 ,艱則吉。

象曰:不能退,不能遂,不祥也。 艱則吉,咎不長也。

 

派︰同學們,派生《十日談》之《 文 》文摘講︰

如何『』 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 語言,『深思』、『細慮』以及『多實驗』或許是個好的起步。…

……

因此現今有人欲察『派生』之『博多』,當如何求『其一』的呢?『物』有『始』『中』『終』,或求『其始』的吧︰

東西』 Object 為本!

這個『阿堵物』彷彿可『比擬』於《易經》之 天地定位

昔 者聖人之作易也,幽贊於神明而生蓍,參天兩地而倚數,觀變於陰陽而立卦,發揮於剛柔而生爻。和順於道德而理於義,窮理盡性以至於命。昔者聖人之作易也,將 以順性命之理,是以立天之道曰陰與陽,立地之道曰柔與剛,立人之道曰仁與義,兼三才而兩之,故易六畫而成卦。分陰分陽,迭用柔剛,故易六位而成章。

天地定位,山澤通氣,雷風相薄,水火不相射,八卦相錯,數往者順,知來者逆,是故易逆數也。

雷以動之,風以散之,雨以潤之,日以烜之,艮以止之,兌以說之,乾以君之,坤以藏之。

追跡『八卦』之祖者乎??

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.
>>> class 八卦(object):
...     def __init__(self):
...         self.祖="太極"
...         self.名="不能問"
...         self.義="不可說"
...     def 相對(self):
...         raise NameError("無法度")
... 
>>> 路人甲=八卦()
>>> 路人甲.祖
'太極'
>>> 路人甲.名
'不能問'
>>> 路人甲.義
'不可說'
>>> 路人甲.相對()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in 相對
NameError: 無法度

>>> class 乾(八卦):
...     def __init__(self):
...         super().__init__()
...         self.名="乾"
...         self.義="天"
...         self.象="☰"
...     def 相對(self):
...         return 坤
... 
>>> 天=乾()
>>> 天.祖
'太極'
>>> 天.名
'乾'
>>> 天.義
'天'
>>> 天.象
'☰'

>>> class 坤(八卦):
...     def __init__(self):
...         super().__init__()
...         self.名="坤"
...         self.義="地"
...         self.象="☷"
...     def 相對(self):
...         return 乾
... 
>>> 地=坤()
>>> 地.祖
'太極'
>>> 地.名
'坤'
>>> 地.義
'地'
>>> 地.象
'☷'
>>> 地.相對().名
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object '乾' has no attribute '名'
>>> 地.相對()().名
'乾'
>>> 地.相對()().義
'天'
>>> 地.相對()().象
'☰'

 

生 ︰《 範 》範說︰程式設計始於『典範』之學習。

python-patterns

A collection of design patterns and idioms in Python.

When an implementation is added or modified, be sure to update this file and rerun append_output.sh (eg. ./append_output.sh borg.py) to keep the output comments at the bottom up to date.

Current Patterns:

Pattern Description
3-tier data<->business logic<->presentation separation (strict relationships)
abstract_factory use a generic function with specific factories
adapter adapt one interface to another using a whitelist
borg a singleton with shared-state among instances
bridge a client-provider middleman to soften interface changes
builder call many little discrete methods rather than having a huge number of constructor parameters
catalog general methods will call different specialized methods based on construction parameter
chain apply a chain of successive handlers to try and process the data
chaining_method continue callback next object method
command bundle a command and arguments to call later
composite encapsulate and provide access to a number of different objects
decorator wrap functionality with other functionality in order to affect outputs
facade use one class as an API to a number of others
factory_method delegate a specialized function/method to create instances
flyweight transparently reuse existing instances of objects with similar/identical state
graph_search (graphing algorithms, not design patterns)
lazy_evaluation lazily-evaluated property pattern in Python
mediator an object that knows how to connect other objects and act as a proxy
memento generate an opaque token that can be used to go back to a previous state
mvc model<->view<->controller (non-strict relationships)
observer provide a callback for notification of events/changes to data
pool preinstantiate and maintain a group of instances of the same type
prototype use a factory and clones of a prototype for new instances (if instantiation is expensive)
proxy an object funnels operations to something else
publish_subscribe a source syndicates events/data to 0+ registered listeners
state logic is org’d into a discrete number of potential states and the next state that can be transitioned to
strategy selectable operations over the same data
template an object imposes a structure but takes pluggable components
visitor invoke a callback for all items of a collection

 

碼 ︰仿 習 。程式初學者,需要多作『模仿』練習,久之 ,熟能生巧。所謂程式『善法』,就是善用其法,因事制宜而已!如果能夠了解什麼是萬物的『性情』︰

『性』 性 一般指得之於天,所以造字時用『生』;『情』 情 得之於環境── 習氣 ──,所以才用『青』,表示後天習得也。故而《三字經》講『性相近,習相遠』。

自然能夠體會程式語言裡講的『物件』之『屬性』和『方法』︰

在程式『思考』中,『性』近『屬性』,物類『固有者』,『情』對應『方法』,或得自於『繼承』,或因『境』而『遷』。

其實是效法『自然之理』,師從『物以類聚』與『遺傳變異』種種之則。不同的『學派』,追求的『重點處』或許『相異』,然而『物件導向』之基本精神還是『一貫相通』的。

實習 機 機板上有一個『蜂鳴器』 Buzzer ,嘗試去『玩』去『改』下面的參考程式

>>> class 蜂鳴器(object):
...     def __init__(self):
...         self.名稱 = "蜂鳴器"
...         self.入出 = "出針"
...         self.針碼 = 11
...         self.邏輯 = "負邏輯"
...         GPIO.setup(11, GPIO.OUT, initial=GPIO.HIGH)
...     def __str__(self):
...         self.名稱 = "蜂鳴器"
...     def 響聲(self):
...         GPIO.output(self.針碼, GPIO.LOW)
...     def 禁聲(self):
...         GPIO.output(self.針碼, GPIO.HIGH)
... 
>>> 音響 = 蜂鳴器()

>>> 音響.響聲()
>>> 音響.禁聲()
>>> 音響
<__main__.蜂鳴器 object at 0x769e8bd0>
>>> repr(音響)
'<__main__.蜂鳴器 object at 0x769e8bd0>'
>>> 音響.名稱
'蜂鳴器'
>>> 音響.邏輯
'負邏輯'
>>> class 哨子聲(蜂鳴器):
...     def __init__(self):
...         super().__init__()
...         self.哨子 = GPIO.PWM(self.針碼, 2)
...     def 響聲(self):
...         self.哨子.start(50)
...     def 禁聲(self):
...         self.哨子.stop()
...         GPIO.output(self.針碼, GPIO.HIGH)
... 
>>> 聲響 = 哨子聲()
>>> 聲響.響聲()
>>> 聲響.禁聲()
>>> 音響.禁聲()
>>> 

 

,思考 Buzzer 聲像什麼?可否演奏『大黃蜂』的呢?也許將會找到程式之不足之處,然後領會

偶然本是發現的圭臬。

 

行 ︰大概真的可擬『聲聲慢』的了。☺

 

訊 ︰☿ ? 人是堅持所信! ☿ ?? 還是信所堅持!!