G
glomde
Hi,
I tried to write a decorator for that should be for methods but for
some reasons
it doens seem to work when you try to do it on the __getattr__ method
in a class.
Could anybody give some hints why this is?
Example:
class decoratorTest(object):
def __init__(self, func):
self.func = func
def __get__(self, instance, cls=None):
print "__get__", instance
self.instance = instance
return self
def __call__(self, *args, **kwds):
return self.func(self.instance, *args, **kwds)
class MyClass1(object):
@decoratorTest
def decoratorTestFunc(self):
print "hello1"
@decoratorTest
def __getattr__(self,name):
print "hello2"
a = MyClass1()
a.decoratorTestFunc() # This works since the __get__ method is called
and the instance is retreived
a.test # This doesnt call the __get__ !!!
Output
__get__ <__main__.MyClass1 object at 0x4012baac>
hello1
Traceback (most recent call last):
File "/home/tonib/test.py", line 27, in ?
a.test
File "/home/tonib/test.py", line 12, in __call__
return self.func(self.instance, *args, **kwds)
AttributeError: 'decoratorTest' object has no attribute 'instance'
I tried to write a decorator for that should be for methods but for
some reasons
it doens seem to work when you try to do it on the __getattr__ method
in a class.
Could anybody give some hints why this is?
Example:
class decoratorTest(object):
def __init__(self, func):
self.func = func
def __get__(self, instance, cls=None):
print "__get__", instance
self.instance = instance
return self
def __call__(self, *args, **kwds):
return self.func(self.instance, *args, **kwds)
class MyClass1(object):
@decoratorTest
def decoratorTestFunc(self):
print "hello1"
@decoratorTest
def __getattr__(self,name):
print "hello2"
a = MyClass1()
a.decoratorTestFunc() # This works since the __get__ method is called
and the instance is retreived
a.test # This doesnt call the __get__ !!!
Output
__get__ <__main__.MyClass1 object at 0x4012baac>
hello1
Traceback (most recent call last):
File "/home/tonib/test.py", line 27, in ?
a.test
File "/home/tonib/test.py", line 12, in __call__
return self.func(self.instance, *args, **kwds)
AttributeError: 'decoratorTest' object has no attribute 'instance'