[ create a new paste ] login | about

Link: http://codepad.org/2w7JVvDB    [ raw code | output | fork ]

Python, pasted on Dec 21:
class meta(type):
	def __new__(cls, name, bases, dict):
		for key, value in dict.items():
			if callable(value) and not key.startswith('__'): 
				dict[key] = decoratorforall(value)
        	return type.__new__(cls, name, bases, dict)

#should be called for every method call in the class
def decoratorforall(methodname):
   def _method(self,*argl,**argd):
        print "2 Inside __call__()"
        returnval = getattr(self, '%s' % methodname)(*argl,**argd)
        print "3 After self.f(*args)"
   	return returnval
   return _method


class Person(object):
	__metaclass__ = meta
	def testprint(self,val):
		print "blah blah"

a = Person()
a.testprint(20)
print "again calling"
a.testprint()


Output:
1
2
3
4
5
6
7
2 Inside __call__()
Traceback (most recent call last):
  Line 24, in <module>
    a.testprint(20)
  Line 12, in _method
    returnval = getattr(self, '%s' % methodname)(*argl,**argd)
AttributeError: 'Person' object has no attribute '<function testprint at 0x4034c4fc>'


Create a new paste based on this one


Comments: