未定義時代入?
perlで言うところの $foo ||= value; 見たいな事 pythonでやろうとするとどうなるんだろう?
>>> class A(object): ... def __init__(self): ... if not self.__dict__.has_key("foo"): ... self.foo = "some value" ... print "I'm %s . foo = %s" % (type(self), self.foo) ... >>> class B(A): ... def __init__(self): ... self.foo = "another value" ... A.__init__(self) ... >>> a = A() I'm <class '__main__.A'> . foo = some value >>> b = B() I'm <class '__main__.B'> . foo = another value
ダサダサ過ぎるorz やっぱダメだ俺・・・
とは言えほかの方法もあんまり思いつかないし、tryは使いたくないし。
そもそもこんな使い方スンナよというのが正解なんだろうけれども。