patternpythonCritical
Decorators with parameters?
Viewed 0 times
parametersdecoratorswith
Problem
I have a problem with the transfer of the variable
but unfortunately, this statement does not work. Perhaps maybe there is better way to solve this problem.
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_functionSolution
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:
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:
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 decoratorHere 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}'.")
Code Snippets
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 decoratorContext
Stack Overflow Q#5929107, score: 1283
Revisions (0)
No revisions yet.