2011-07-09から1日間の記事一覧

propertyの継承(getterのみ -> getter,setter両方提供)

propertyは以下のような感じで使える。 例えば、渡された数値を整数値として扱いたいようなオブジェクトの例。 class Obj(object): def __init__(self): self._n = None @property def n(self): return self._n @n.setter def n(self, v): self._n = abs(v) …

pythonのスコープ

エラーになる。 def f(): x = 0 def g(): x += 1 print x g() f() # UnboundLocalError: local variable 'x' referenced before assignment primitiveな値ではなく、オブジェクトなどの参照を保持する値を使えば大丈夫. class Ref(object): def __init__(sel…