https://pyformat.info/
Something to consider in using the newer .format() (it was introduced in the later stages of Python 2, I believe) is to use keywords and predefining the string and format as class variables.
basic_descr2_str = "The name is: {name}, this character's melee \
capabilities are {melee}, agility seems to be pretty {agility}, he uses \
{magic} magic, and has {luck} luck."
basic_descr2_format = (
name = self.name,
melee = self.melee,
agility = self.agility,
magic = self.magic,
luck = self.luck
)
def basic_description_2(self):
print(basic_descr_2_str.format(*basic_descr2_format)) #unpack argument sequence
The payoff of this approach is good in terms of keeping clutter out of the instance methods.
It is untested and pure speculation at this point, but you get the idea. Perhaps custom objects will be needed. I’ll continue to explore this while you test this code and report back (please).
With this mockup I found a fatal error…
class Player:
basic_descr2_str = "The name is: {name}, this character's melee \
capabilities are {melee}, agility seems to be pretty {agility}, he uses \
{magic} magic, and has {luck} luck."
basic_descr2_format = (
name = self.name, # syntax error
melee = self.melee,
agility = self.agility,
magic = self.magic,
luck = self.luck
)
def __init__(self, name, melee, agility, magic, luck):
self.name = name,
self.melee = melee,
self.agility = agility,
self.magic = magic,
self.luck = luck
def basic_description_2(self):
print(self.basic_descr_2_str.format(*self.basic_descr2_format))
dorf = Player('dorf', 5, 10, 15, 20)
dorf.basic_description_2()
Reverting somewhat at least gives a result, but I have no explanation as to why it is so weird…
class Player:
basic_descr2_str = "The name is: {name}. this character's melee \
capabilities are {melee}, agility seems to be pretty {agility}, he uses \
{magic} magic, and has {luck} luck."
def __init__(self, name, melee, agility, magic, luck):
self.name = name,
self.melee = melee,
self.agility = agility,
self.magic = magic,
self.luck = luck
def basic_description_2(self):
print(self.basic_descr2_str.format(
name=self.name,
melee=self.melee,
agility=self.agility,
magic=self.magic,
luck=self.luck
))
dorf = Player('dorf', 5, 10, 15, 20)
dorf.basic_description_2()
=============== RESTART: D:/cc/discuss/users/kekpop/player.py ===============
The name is: ('dorf',). this character's melee capabilities are (5,), agility seems to be pretty (10,), he uses (15,) magic, and has 20 luck.
>>>
Invited @appylpye to shed some light on this.