0

I am trying to write a decorator in my python codes. When I compile it in the Jupyter notebook it runs fine but when I run the codes in the Spyder IDE, I get an error.

def search_func(sheetname):
    def insider(f):
        file = openpyxl.load_workbook("Excelfile.xlsx")
        current_sheet = file[sheetname]
        f(current_sheet)
    return insider

@search_func('Passwords')
def Longin(current_sheet):
    Name = User_name.get() + str(Password.get())
    for i in range(1,current_sheet.max_row +1):
       for j in range(1,current_sheet.max_column+1):
           if current_sheet.cell(i,j).value == Name:
               print("Hello")

The error, I get is "Longin() missing 1 required positional argument: 'current_sheet'"

Can anyone help me please?

Appreciate your time.

Cheers

user3064089
  • 65
  • 1
  • 3
  • 10

1 Answers1

1

You swapped the decorator function and args at the decorator declarations.

The outer function should get the f while the inner gets the sheetname.

def decorator(function):
        def wrapper(*args, **kwargs):
            funny_stuff()
            something_with_argument(argument)
            result = function(*args, **kwargs)
            return result
        return wrapper

The example taken from decorator with arguments. Its also show how to create a decorator that get arguments.

Amiram
  • 1,227
  • 6
  • 14