質問

以下の非常に簡単な例のようにしています。こういうことができるように使用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

どのようにしていない可能ですか?のようにしてるわけではありませんので間違いだったのか?

役に立ちましたか?

解決

ありが失わなければならなかったからClassX.以下ではないかなと思ったのがきっかけcollegueにしてくれたことなんですが融合したかのような醜.ができております。

まいそういう設定のプロキシオブジェクト。ことになる可がより良いソリューションよりご同僚のもとを考えているのか、それが容易になるだけでパッチの一部追加。これは動作しませんの授業など bool, は、お客さまのユーザ定義されたクラス

def HTMLDecorator (obj):
    def html ():
        sep = cgi.escape (repr (obj))
        return sep.join (("<H1>", "</H1>"))
    obj.html = html
    return obj

このプロキシーのバージョン:

class HTMLDecorator(object):
    def __init__ (self, wrapped):
        self.__wrapped = wrapped

    def html (self):
        sep = cgi.escape (repr (self.__wrapped))
        return sep.join (("<H1>", "</H1>"))

    def __getattr__ (self, name):
        return getattr (self.__wrapped, name)

    def __setattr__ (self, name, value):
        if not name.startswith ('_HTMLDecorator__'):
            setattr (self.__wrapped, name, value)
            return
        super (HTMLDecorator, self).__setattr__ (name, value)

    def __delattr__ (self, name):
        delattr (self.__wraped, name)

他のヒント

両方のジョンソリューションいます。別のオプションできるHTMLDecoratorは非常にシンプルでモンキー-パッチでの拠点としてクラスです。この作品だけをユーザ定義の授業は、組み込みタイプ:

import cgi

class ClassX(object):
    pass # ... with own __repr__

class ClassY(object):
    pass # ... with own __repr__

inst_x=ClassX()
inst_y=ClassY()

class HTMLDecorator:
    def html(self): # an "enhanced" version of __repr__
        return cgi.escape(self.__repr__()).join(("<H1>","</H1>"))

ClassX.__bases__ += (HTMLDecorator,)
ClassY.__bases__ += (HTMLDecorator,)

print inst_x.html()
print inst_y.html()

は注意が必要なもの--モンキ-パッチのようなことでも高い価格で読みやすさと保守性のコードです。に戻る場合はこのコードの一年後に大きな困難うごClassXっとhtml()メソッドの場合、ClassXが定義されるその他の図書館があります。

どのようにしていない可能ですか?のようにしてるわけではありませんので間違いだったのか?

それは確かに可能です。どうしたのであること HTMLDecorator.__init__() います。

こちらは簡単な例:

def decorator (func):
    def new_func ():
        return "new_func %s" % func ()
    return new_func

@decorator
def a ():
    return "a"

def b ():
    return "b"

print a() # new_func a
print decorator (b)() # new_func b

@ジョン(37448):

だいご質問、ご相談などお気軽に名(悪い。んを探してデコレータ機能は何でデコレータです。私た後は、html(self)def利用ClassXまたは高級感の __repr__.私は、この仕事を修正することなくClassXまたは登.

ああ、その場合は、そのようにします。なんといいデコレータが示すのはどのように引数を渡すクラスの初期化関数で取得するその引数です。

import cgi

class ClassX(object):
    def __repr__ (self):
        return "<class X>"

class HTMLDecorator(object):
    def __init__ (self, wrapped):
        self.__wrapped = wrapped

    def html (self):
        sep = cgi.escape (repr (self.__wrapped))
        return sep.join (("<H1>", "</H1>"))

inst_x=ClassX()
inst_b=True

print HTMLDecorator(inst_x).html()
print HTMLDecorator(inst_b).html()

@ジョン(37479):

ありが失わなければならなかったからClassX.以下ではないかなと思ったのがきっかけcollegueにしてくれたことなんですが融合したかのような醜.ができております。

import cgi
from math import sqrt

class ClassX(object): 
  def __repr__(self): 
    return "Best Guess"

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

avoid="__class__ __init__ __dict__ __weakref__"

class HTMLDecorator(object):
    def __init__(self,master):
        self.master = master
        for attr in dir(self.master):
            if ( not attr.startswith("__") or 
                attr not in avoid.split() and "attr" not in attr):
                self.__setattr__(attr, self.master.__getattribute__(attr))

    def html(self): # an "enhanced" version of __repr__
        return cgi.escape(self.__repr__()).join(("<H1>","</H1>"))

    def length(self):
        return sqrt(sum(self.__iter__()))

print HTMLDecorator(inst_x).html()
print HTMLDecorator(inst_y).html()
wrapped_z = HTMLDecorator(inst_z)
print wrapped_z.length()
inst_z[0] += 70
#wrapped_z[0] += 71
wrapped_z.__setitem__(0,wrapped_z.__getitem__(0)+ 71)
print wrapped_z.html()
print HTMLDecorator(inst_b).html()

出力:

<H1>Best Guess</H1>
<H1><__main__.ClassY object at 0x891df0c></H1>
70.0
<H1>[141, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576]</H1>
<H1>True</H1>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top