generators as decorators simple issue

pyjoshsys j.m.dagenhart at gmail.com
Wed Sep 12 06:22:31 EDT 2012


The output is still not what I want. Now runtime error free, however the output is not what I desire.



def setname(cls):
    '''this is the proposed generator to call SetName on the object'''
    
    try:
        cls.SetName(cls.__name__)
    except Exception as e:
        print e
    finally:
        return cls

class Trial(object):
    '''class to demonstrate with'''
    def __init__(self):
        object.__init__(self)
        self.name = None
    
    @classmethod
    def SetName(cls, name):
        cls.name = name

@setname
class Test(Trial):
    '''i want SetName to be called by using setname as a decorator'''
    def __init__(self):
        Trial.__init__(self)

        

if __name__ == '__main__':
    test = Test()
    print 'instance'
    print '', test.name #should be Test
    print 'class'
    print '', Test.name
    

The output is: python decors2.py 
instance
 None
class
 Test

I want: 
instance
 Test
class
 Test

Is this possible in this manner?



More information about the Python-list mailing list