Python.use(better) #真理値表: step06 -- def operator(self, op)

記事一覧 Python.use(better)《Python3.1》

def operator(self, op)

《著》森こねこ、小粒ちゃん+∞《監修》小泉ひよ子とタマゴ倶楽部
第0版♪2001/03/02 ● 第1版♪2003/05/25 ● 第2版♪2004/06/01 ● 第3版♪2009/02/28

課題を作成する過程を通して「論理演算/文字列処理」の理解を深めます。
※ Python1.5 で作成した例題を、Python3.1 で再構成しました。

事例:モジュールを起動する

■ 全項目を確認する

全ステップの「項目」を確認するには、関数 do を利用します。

$ python -i TruthTable.py
>>> do()
...
6: step06 -- def operator(self, op):
■ 各項目を実行する

各ステップの「動作」を確認するには、関数 do に実引数を指定します。

>>> do(6)
>>> # -------------------------------------------------- step06
>>> t = TruthTable(); t
 p | q | p _ q
                            • -
>>> t.operator("and") p | q | p and q
                                • -
T | T | T T | F | F F | T | F F | F | F >>> t.operator("or") p | q | p or q
                              • -
T | T | T T | F | T F | T | T F | F | F >>>

クラス TruthTable のインスタンス t を生成した後で、メソッド operator を利用すると、

  • 論理積 and を表わす、真理値表が出力されます。
  • 論理和 or を表わす、真理値表が出力されます。

事例:コードの解説

    class TruthTable(object):
        ...
        def _body(self):
            B = "TF"
            return [" %s | %s | %s\n"%(B[i//2], B[i%2], e.center(self.width))
                for i,e in enumerate(self)]
        def _header(self):
            s = "p %s q"%self.op
            self.width = len(s)
            ss = " p | q | %s\n"%s
            self.length = len(ss)
            return [ss]

        ## ----------------------------------------
        def operator(self, op):
            self.op = op
            B = self.boolean
            self.table = [eval("b1 %s b2"%op)
                for b1 in B for b2 in B]
            return self

    ## ----------------------------------------
    local = locals()
    ex_TruthTable1(local)
《余録》テストケース
    def ex_TruthTable(local):                        #@:
        source = '''
t = TruthTable(); t
t.operator("and")
t.operator("or")
'''.split("\n")

        do_it(source, local)
■ #1: メソッド:__repr__

メソッド __repr__ は、組み込み関数 repr に呼応して、オブジェクトに固有の文字列表現を与えます。そこで、補助関数として、次の

  • _body
  • _header

を用意します。

        def _body(self):
            B = "TF"
            return [" %s | %s | %s\n"%(B[i//2], B[i%2], e.center(self.width))
                for i,e in enumerate(self)]

メソッド _body は、真理値表の本体を規定します。

  • 各項目 e(長さ1の文字列)を、幅 self.width で中央揃え center にします。
        def _header(self):
            s = "p %s q"%self.op
            self.width = len(s)
            ss = " p | q | %s\n"%s
            self.length = len(ss)
            return [ss]

メソッド _header は、真理値表のヘッダーを規定します。

  • インスタンス属性 self.width は、項目の幅(文字数)を保持します。
  • インスタンス属性 self.length は、ヘッダーの長さ(文字数)を保持します。
■ #2: 組み込み関数:eval
    ## ---------------------------------------- before
        def op_and(self):
            self.op = "and"
            B = self.boolean
            self.table = [b1 and b2
                for b1 in B for b2 in B]
            return self

        def op_or(self):
            self.op = "or"
            B = self.boolean
            self.table = [b1 or b2
                for b1 in B for b2 in B]
            return self
    ## ---------------------------------------- after
        def operator(self, op):
            self.op = op
            B = self.boolean
            self.table = [eval("b1 %s b2"%op)
                for b1 in B for b2 in B]
            return self

メソッド operator は、メソッド op_and/op_or に共通するコードを抽出(リファクタリング)したものです。

  • 組み込み関数 eval を用いて、実行時に確定したコードの断片を評価(実行)します。

《Note》組み込み関数 eval:実行したいコードの断片は、実行時に確定します。それを eval で評価すると、動的な問題解決が可能になります。

》こちらに移動中です《
TOP


関連記事

Last updated♪2009/12/14