Rahul
Rahul

Reputation: 1627

Unable to use decorator defined in other file in python

I am trying to use decorator for printing my logs. And to do this, I have defined decorator in one file named custom_logger.py :

import logging

class Logger(object):
   def __init__(self,decoratee_enclosing_class):
        self.decoratee_enclosing_class = decoratee_enclosing_class
   def __call__(self, aFunc):
      """Trace entry, exit and exceptions."""
      def loggedFunc( *args, **kw ):
         print "enter", aFunc.__name__
         try:
            result= aFunc( *args, **kw )
         except Exception, e:
            print "exception", aFunc.__name__, e
            raise
         print "exit", aFunc.__name__
         return result
         loggedFunc.__name__= aFunc.__name__
         loggedFunc.__doc__= aFunc.__doc__
         return loggedFunc

And here is my sample test code :

from custom_logger import Logger

class Test(object):
   @Logger('Test')
   def testP(self):
      print "hello"
a = Test()
a.testP()

I am getting following error : Traceback (most recent call last): File "test.py", line 13, in a.testP() TypeError: 'NoneType' object is not callable

So can any one point out what am I missing ?

I have followed this link for reference.

Upvotes: 1

Views: 1769

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599876

You have an indentation error in your decorator. The last three lines of the __call__ method should be at the same indentation as the def loggedFunc line.

Upvotes: 3

Related Questions