generators as decorators simple issue

j.m.dagenhart at gmail.com j.m.dagenhart at gmail.com
Tue Sep 11 22:28:10 EDT 2012


I'm trying to call SetName on an object to prevent me from ever having to call it explictly again on that object. Best explained by example.


def setname(cls):
    '''this is the proposed generator to call SetName on the object'''
    try:
        cls.SetName(cls.__name__)
    finally:
        yield cls


class Trial:
    '''class to demonstrate with'''
    def SetName(self, name):
        print 1, 1

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

        print 'Yay! or Invalid.'

if __name__ == '__main__':
    test = Test()


How can i fix this? 
This is my exact error: python decors2.py 
Traceback (most recent call last):
  File "decors2.py", line 23, in <module>
    test = Test()
TypeError: 'generator' object is not callable



More information about the Python-list mailing list