2015-03-26から1日間の記事一覧

python メタクラスを使ってクラスプロパティを実装する

pythonにはpropertyという組み込み関数が用意されており、これを使うことで(擬似的に)プライベートなインスタンス変数に対するgetter/setterのようなメソッドを作成する事ができる。 class Hoge: def __init__(self, name): self.__name = name @property …

python デコレータを使って例外処理をモジュール化する

pythonで普通に例外処理を書くと、以下のようになる。 def hoge(*args): try: # 処理 except Exception as e: # 例外処理 return def fuga(*args): try: # 処理 except Exception as e: # 例外処理 return def piyo(*args): try: # 処理 except Exception as…