Re: generators as decorators simple issue

2012-09-12 Thread pyjoshsys
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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: generators as decorators simple issue

2012-09-12 Thread pyjoshsys
so decorators only pass the object and not any instance of the object as the 
implied argument? Is this right?

The idea was to use @setname instead of instance.SetName(instance.__name__).

I thought decorators would do this, but it seems not.







-- 
http://mail.python.org/mailman/listinfo/python-list