How create decorator inside a class?

Catanzaro

I want create a simple decorator function that get in input a func e add some methode before. Here an example:

class A:

    def beforeWriteOracle(func):
        def wrapper(self, func):
            self.dbOracle.truncateTable(self.oracle_final_table)
            func(self.mylist)
        return wrapper

    @beforeWriteOracle
    def writeDataToOracle(self, writeDataToOracleRequestList): 
        return self.executeMethod(self.writeDataToOracleImpl, writeDataToOracleRequestList, threaded, False)

    self.writeDataToOracle(self, writeDataList)

but i have error:

"beforeWriteOracle() missing 1 required positional argument: 'func'"

How use correctly decorator for my case?

thanks

chepner

You don't need to (or want to) pass func as an argument to the wrapper; the wrapper should take the same arguments that function you are decorating takes, since it's going to "become" that function.

func itself is available as a non-local variable inside wrapper, which is a closure: it retains the value passed to beforeWriteOracle even after beforeWriteOracle exits.

def beforeWriteOracle(func):
    def wrapper(self, *args, **kwargs):
        self.dbOracle.truncateTable(self.oracle_final_table)
        func(self, *args, **kwargs)
    return wrapper


@beforeWriteOracle
def writeDataToOracle(self, writeDataToOracleRequestList):
    return self.executeMethod(self.writeDataToOracleImpl, writeDataToOracleRequestList, threaded, 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 create Jpa repository dynamically inside a class?

how to use decorator in a class

Functionality inside abstract decorator class instead of decorators

How to create a class inside Microsoft Azure Function?

How to create an object inside class static method

Decorator inside a class throw warning in Pycharm

How to create a decorator that adds a single function to the component class?

define a decorator as method inside class

Difficulty getting decorator to work inside class in python

Typescript - calling class methods inside constructor decorator

decorator inside class Python

Extending a class in Python inside a decorator

How to create a thread inside a class?

how to create a decorator for a method

Defining a class inside a function to interrupt decorator execution

How to declare decorator inside a class, to decorate an already decorated, inherited method?

python class decorator inside method

Access getters inside the Vue Class Component decorator

How to create a class decorator that can add multiple methods to a class?

How to create @login_required decorator for app engine class

How do I move this method-decorator pattern inside a class?

how to create a enum property inside a class with typescript?

How to create a Class inside of a namespace?

How to create a cooldown decorator

How to create function inside class in javascript?

How to define method to act as decorator for other methods inside the same class in Python?

How to create a decorator in flask?

How to create an array inside an object/class in JAVA?

How to add decorator to dynamically create class