2012-03-28 23 views
14

के रूप में परिभाषित करके एक वर्ग को सजाने के लिए क्या कोई इस का एक सरल उदाहरण दिखा सकता है? मैं प्राप्त करने के लिए कक्षाओं कार्यों नहीं का उपयोग कर के रूप में ब्रूस एकेल hereपायथन: सजावट को कक्षा

निम्नलिखित काम करता है बताते हैं सिवाय क्या अजगर 2.6 में लागू किया गया है PEP 3129 का उपयोग कर कोशिश कर रहा हूँ:

class Decorator(object): 
    def __init__(self, arg): 
     self.arg = arg 

    def __call__(self, cls): 
     def wrappedClass(*args): 
      return cls(*args) 
     return type("TestClass", (cls,), dict(newMethod=self.newMethod, classattr=self.arg)) 

    def newMethod(self, value): 
     return value * 2 

@Decorator("decorated class") 
class TestClass(object): 
    def __init__(self): 
     self.name = "TestClass" 
     print "init %s"%self.name 

    def TestMethodInTestClass(self): 
     print "test method in test class" 

    def newMethod(self, value): 
     return value * 3 

छोड़कर, ऊपर में, wrappedClass एक नहीं है कक्षा लेकिन एक वर्ग प्रकार वापस करने के लिए एक समारोह में छेड़छाड़ की। मैं निम्नानुसार कॉल करने योग्य लिखना चाहता हूं:

def __call__(self, cls): 
     class wrappedClass(cls): 
      def __init__(self): 
       ... some code here ... 
     return wrappedClass 

यह कैसे किया जाएगा? संपादित: मैं पूरी तरह से यकीन है कि क्या में "" "... कुछ कोड यहाँ ..." "" चला जाता है

+0

क्या आपने अपना कोड पोस्ट करने का प्रयास किया था? यह काम करना चाहिए। –

+0

फ़ंक्शन का उपयोग करने वाला पहला भाग काम करता है। मैं लपेटा क्लास को असली कक्षा के रूप में कैसे लिखूं? – tsps

+1

आपका सजावटी क्या करना चाहिए? मैं आपको यह नहीं बता सकता कि यह कोड क्या है, यह जानने के बिना "यहां कुछ कोड" में कौन सा कोड जाना है। –

उत्तर

13

आप, new_method() अधिलेखित करना चाहते हैं, तो बस यह नहीं कर रहा हूँ:

class Decorator(object): 
    def __init__(self, arg): 
     self.arg = arg 
    def __call__(self, cls): 
     class Wrapped(cls): 
      classattr = self.arg 
      def new_method(self, value): 
       return value * 2 
     return Wrapped 

@Decorator("decorated class") 
class TestClass(object): 
    def new_method(self, value): 
     return value * 3 

यदि आप __init__() को बदलना नहीं चाहते हैं, तो आपको इसे ओवरराइट करने की आवश्यकता नहीं है।

2

इस के बाद, वर्ग NormalClass एक ClassWrapper उदाहरण हो जाता है:

def decorator(decor_arg): 

    class ClassWrapper: 
     def __init__(self, cls): 
      self.other_class = cls 

     def __call__(self,*cls_ars): 
      other = self.other_class(*cls_ars) 
      other.field += decor_arg 

    return ClassWrapper 

@decorator(" is now decorated.") 
class NormalClass: 
    def __init__(self, name): 
     self.field = name 

    def __repr__(self): 
     return str(self.field) 

टेस्ट:

if __name__ == "__main__": 

    A = NormalClass('A'); 
    B = NormalClass('B'); 

    print A 
    print B 
    print NormalClass.__class__ 

आउटपुट:

एक अब सजाया गया है।
बी अब सजाया गया है।
__main __। क्लासवापर

+2

आप __call__ विधि के अंदर 'अन्य' चर वापस करने के लिए भूल गए –

संबंधित मुद्दे