![]() |
Variable Scopes Decorators - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Variable Scopes Decorators (/thread-32005.html) |
Variable Scopes Decorators - oclmedyb - Jan-14-2021 Hello, I try to write some Excel functions with xlwings and openpyxl. For example one function will get details about an item from another Excel file. Everytime a function is run, it has to open that other Excel file, do its thing and close the file again. Since I will have many functions that work with other Excel files, I thought I'll do that with an decorator. I don't have to code right here but it looks something like this: def excel_wrapper(func): def inner(*args, **kwargs): open_excel_file('filename') dict1 = {...} returnvalue = func(*args, **kwargs) close_excel_file() return returnvalue return inner @excel_wrapper def get_value(item, value): for x in dict1 ... get_value('banana', 'color')This results in a NameError because dict1 is not defined. I tried to move the open_excel_file etc. stuff from the inner function to the decorator function but this gave me the same error.Since it is not a single variable but several ones, I guess I don't want to make them global. RE: Variable Scopes Decorators - Gribouillis - Jan-14-2021 You can just add dict1 to the parameters def excel_wrapper(func): def inner(*args, **kwargs): open_excel_file('filename') dict1 = {...} returnvalue = func(dict1, *args, **kwargs) close_excel_file() return returnvalue return inner @excel_wrapper def get_value(dict1, item, value): for x in dict1 ... get_value('banana', 'color') RE: Variable Scopes Decorators - oclmedyb - Jan-14-2021 I can try this earliest tomorrow, but are you sure that this will work? From my understanding this should result in the same error since dict1 will be defined after def get_value(dict1, item, value):
RE: Variable Scopes Decorators - Gribouillis - Jan-14-2021 I'm sure it will work. The dict1 in the function is only a parameter. It is passed only when the function is called. RE: Variable Scopes Decorators - oclmedyb - Jan-15-2021 Hey, I just wanted to reply that it is working like this. And it also makes sense to me if I think about it. ![]() But lets assume it is not only dict1 but 10 variables I need to pass in, is there a easier/better/prettier way than passing them as 10 arguments?
RE: Variable Scopes Decorators - Gribouillis - Jan-15-2021 oclmedyb Wrote:is there a easier/better/prettier way than passing them as 10 arguments?Yes you can pass a single class instance containing the variables. from functools import wraps class Foo: def __init__(self): self.spam = 1 self.eggs = 23 self.ham = 42 def excel_wrapper(func): foo = Foo() @wraps(func) def inner(x, y): return func(foo, x, y) return inner @excel_wrapper def func(foo, x, y): print(foo.eggs + x + foo.ham * y) func(1000, 2000) RE: Variable Scopes Decorators - oclmedyb - Jan-15-2021 Yes this could be useful. Thank you very much! |