私が使用することを余儀なくされている sqlobject バージョンは__unicode__
、カスタム文字列のような型に魔法を期待しています。フォーム文字列を継承してその要件を満たそうとしています:
class Foo(str):
extra_attribute = 42
def __repr__(self):
return repr(self) # optional
def __unicode__(self):
return unicode(self) # infinite recursion
return super(Foo, self).__unicode__() # str doesn't have __unicode__
return unicode(str(self)) # an ugly hack, but works
最後の行は私ができる最善のことですか、それともよりクリーンな方法はありますか?
Python 2.x で Unicode ライクなオブジェクトを変換する明らかに適切な方法は次のとおりです。
return unicode(x)
または、より冗長に:
if hasattr(x, "__unicode__"):
return x.__unicode__()
else:
return unicode(x)
残念ながら、私が使用しなければならない sqlobject バージョンではそれができません。