0

I am writing a web based text rpg as a way to practice and learn flask. This has resulted in me having render_template() functions that list a large number of variables. I usually find that when code looks very sloppy there is probably a better way to do it. All of the variables are defined in the function above, but then need to be defined again when given to render_template. Is there a better way to do this?

    return render_template('rpg_play.html', game_name=pname, pname=pname, ability_1=ability_1, ability_2=ability_2, ability_3=ability_3, ability_4=ability_4, ammo_1=ammo_1, ammo_2=ammo_2, ammo_3=ammo_3, ammo_4=ammo_4, heal_pot=heal_pot, fire_pot=fire_pot, current_hp=current_hp, max_hp=max_hp, gold=gold, debt=debt, weapon=weapon, pstatus=pstatus, role=role, level=level, experience=experience, text=text, choice_type=choice_type, choice1=choice1, choice2=choice2, choice3=choice3, choice1_text=choice1_text, choice2_text=choice2_text, choice3_text=choice3_text, choice1_destination=choice1_destination, choice2_destination=choice2_destination, choice3_destination=choice3_destination)
Strayer
  • 31
  • 4

1 Answers1

0

Yup. Star expressions let you unpack a dict as named arguments:

def print_args(arg1, arg2, arg3):
    print(arg1, arg2, arg3)

d = {'arg1': 'a', 'arg2': 4, 'arg3': 'foo'}
print_args(**d)  # a 4 foo

So you'd have a dict with the argument names as keys and pass that with the **. Read more about starred expressions here.