how to use decorator in a class

Spybdai

I know there is similar question, but my scenario is somehow different: refer to codes:

class MyClass(object):
    def __init__(self, log_location)
        self.logs = logging(log_location) # create log object by the log_location, this object should be used by the decorator fucntion

    def record_log(log_object): 
        """ this is the decorator function
        """
        def deco(func):
            def wrap(*args, **kwargs):
                rs = func()

                # use log object to record log
                if rs:
                    log_object.record('success')
                else:
                    log_object.record('fail')

            return wrap
        return deco

   @record_log(self.logs) 
   def test(self):
       rs = do_some_thing
       if rs:
            return True
       return False

def main():
    my_class = MyClass()
    my_class.test()   

But, there is an error like this:

@record_log(self.logs)
NameError: name 'self' is not defined

Hos should I use the instance attribute self.logs in a decorator function in such scenario like this??

Thanks very much!

Vadim Landa

There are several objections about your code:

  1. deco() is redundant. You can directly return wrap from record_log().

  2. If you only plan to decorate MyClass's methods, then there is no point in passing log_object to the decorator, as self.logs will always be used. Otherwise, consider moving the decorator to module level, as already suggested by others.

  3. The decorated method's return value is currently lost.

  4. The call to the decorated function does not pass self to it.

The proper code would therefore be:

class MyClass(object):
    def __init__(self, log_location):
        self.logs = logging(log_location)

    def record_log(func):
        """ this is the decorator function
        """
        def wrap(self):
            rs = func(self)
            # use log object to record log
            if rs:
                print 1
                self.logs.record('success')
            else:
                print 2
                self.logs.record('fail')
            return rs
        return wrap

    @record_log
    def test(self):
       rs = do_some_thing
       if rs:
            return True
       return False

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use the Class decorator wrapper?

how to use decorator in class method

How to use a decorator of a descriptor within a sub class

how to use decorator pattern to directly modify members of base class?

How to use the @shared_task decorator for class based tasks

How to use the user_passes_test decorator in class based views?

Django how to use the ``receiver`` decorator on a class instead on a function

How to use a Class Decorator in Typescript to modify all of the Classes' Static Methods?

Use a property decorator to initiate a class

How to use `@dataclass` decorator within a decorator?

how to pass arguments to a decorator in a class

How create decorator inside a class?

How to apply a decorator to existing class

How to define decorator within the class?

How to use @Component decorator in both abstract class and in the implementation of abstract class in Angular 8?

How to apply a decorator to all class methods using class decorator

How to use a decorator with a coroutine in Python?

how to use python decorator with argument?

How to use decorator in rails correctly?

Use an instance method as a decorator within another class

Use a class decorator to alter subclass methods

How to implement "decorator" functionality in a "class" object?

How to dynamically add decorator to class in typescript

How to express type for output class of the TS decorator

How to implement Python decorator with arguments as a class?

How do you type a class decorator in typescript?

How do I build this `class decorator`?

How to access class metadata from method decorator

How to add parameters to a class decorator in TypeScript?