["python","function","parameters","arguments","decorator"]
Decorators with parameters?

I have a problem with the transfer of the variable insurance_mode by the decorator. I would do it by the following decorator statement: @execute_complete_reservation(True) def test_booking_gta_object(self): self.test_select_gta_object() but unfortunately, this statement does not work. Perhaps maybe there is better way to solve this problem. def execute_complete_reservation(test_case,insurance_mode): def inner_function(self,*args,**kwargs): self.test_create_qsf_query() test_case(self,*args,**kwargs) self.test_select_room_option() if insurance_mode: self.test_accept_insurance_crosseling() else: self.test_decline_insurance_crosseling() self.test_configure_pax_details() self.test_configure_payer_details return inner_function
The syntax for decorators with arguments is a bit different - the decorator with arguments should return a function that will take a function and return another function. So it should really return a normal decorator. A bit confusing, right? What I mean is: def decorator_factory(argument): def decorator(function): def wrapper(*args, **kwargs): funny_stuff() something_with_argument(argument) result = function(*args, **kwargs) more_funny_stuff() return result return wrapper return decorator Here you can read more on the subject - it's also possible to implement this using callable objects and that is also explained there. Usage: @decorator_factory("Some argument") def function_to_be_decorated(args): print(f"Do something with '{args}'.") decorator_factory("Some argument") uses the given argument to create a standard, argumentless decorator. So the following block is functionally identical to the one above: created_decorator = decorator_factory("Some argument") @created_decorator def function_to_be_decorated(args): print(f"Do something with '{args}'.")