I need to create/declare 50 functions inside my Class. But, I need to do it dynamically with custom function names and cutom urls in the function body. Something as the following:
for item_nr in range(1, 51):
@task(1)
def view_item_with_id_{item_nr}(self, item_nr=item_nr):
self.client.get(
url=f"/my-url/id=SEPT24_00{item_nr}",
verify=False,
auth=(os.environ['USERNAME'], os.environ['PASSWORD'])
)
P.S since it's inside a class- I cannot really use another function to generate it as suggested in some other threads, because the 'self' parameter will not be visible then. Example (this will not work):
def function_builder(args):
def function(more_args):
#do stuff based on the values of args
return function
Any help is appreciated
You could use setattr to define new class methods:
Out: