Pythonのお勉強 Part 27at TECH
Pythonのお勉強 Part 27 - 暇つぶし2ch268:デフォルトの名無しさん
08/06/24 22:29:30
>>258
「Python的に正しい」かどうかは知らんけど、こういうのはどう?
分かりにくくなるだけのような気がするが

class Delegator:
    def __init__(self):
        self.__dict__ = { 'object': None }
    def set(self, obj):
        self.__init__()
        self.object = obj
        for attr in dir(obj):
            if attr != '__class__':
                setattr(self, attr, getattr(obj, attr))

def re_search_x(regexp, s, delegator):
    m = regexp.search(s)
    if not m: return False
    delegator.set(m)
    return True

if __name__ == '__main__':
    import sys, re
    re_a = re.compile('a')
    re_b = re.compile('b')
    m = Delegator()
    for s in iter(sys.stdin.readline, ""):
        if re_search_x(re_a, s, m):
            print "a: matched at %d" % m.start()
        elif re_search_x(re_b, s, m):
            print "b: matched at %d" % m.start()


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch