2008年6月6日金曜日

ふしだらなclass

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
ちょっと寿命が縮みそうなクラスです。正しく使えば強力でしょうが。
数行のスクリプトを書くならこの方が幸せかな。

In [3]: class Hoge(object):
...: def __setattr__(self, name, value):
...: self.__dict__.update({name: value})
...:
...:

In [4]: h = Hoge()

In [5]: h.x =1

In [6]: h.
h.__class__ h.__init__ h.__setattr__
h.__delattr__ h.__module__ h.__str__
h.__dict__ h.__new__ h.__weakref__
h.__doc__ h.__reduce__ h.x
h.__getattribute__ h.__reduce_ex__
h.__hash__ h.__repr__

In [6]: h.x
Out[6]: 1

0 件のコメント: