以下私は何をしようとしているかの非常に簡単な例を持っています。他のクラスでHTMLDecoratorを使用できるようにしたい。それがデコレータと呼ばれるという事実を無視して、ただの名前です。既存のインスタンスからメソッドを継承するにはどうすればよいですか?
import cgi
class ClassX(object):
pass # ... with own __repr__
class ClassY(object):
pass # ... with own __repr__
inst_x=ClassX()
inst_y=ClassY()
inst_z=[ i*i for i in range(25) ]
inst_b=True
class HTMLDecorator(object):
def html(self): # an "enhanced" version of __repr__
return cgi.escape(self.__repr__()).join(("<H1>","</H1>"))
print HTMLDecorator(inst_x).html()
print HTMLDecorator(inst_y).html()
wrapped_z = HTMLDecorator(inst_z)
inst_z[0] += 70
wrapped_z[0] += 71
print wrapped_z.html()
print HTMLDecorator(inst_b).html()
出力:
Traceback (most recent call last): File "html.py", line 21, in print HTMLDecorator(inst_x).html() TypeError: default __new__ takes no parameters
は、私は可能何をしようとしていますか?もしそうなら、私は間違って何をしていますか?