0

My class is:

class Champion:

    def __init__(self, secondary_bar, secondary_attributes, health, health_reg, mana, mana_reg, c_range, att_damage, att_speed, armor, magic_res, move_speed):
        self.secondary_bar = secondary_bar 
        self.secondary_attributes = secondary_attributes
        self.health = health
        self.health_reg = health_reg
        self.mana = mana
        self.mana_reg = mana_reg
        self.c_range = c_range
        self.att_damage = att_damage
        self.att_speed = att_speed
        self.armor = armor
        self.magic_res = magic_res
        self.move_speed = move_speed

    ...


class SpecificChampion(Champion):

    ...


my_champ = SpecificChampion("mana", "melee", 500, 20, 250, 20, 125, 50, 0.8, 65, 65, 320)

I wanted a shorter way to create the first class, since it takes too much space in my opinion. I tried to store them in a list using kwargs, but it was kind of confusing to access them and pass them as parameters to other functions. Any advice?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
ecoH
  • 23
  • 2
  • 7
  • 1
    use `**args` or `**kwargs` – Stack Jan 06 '18 at 14:49
  • Just break the long logical line across multiple physical lines? There isn't much you can do there. – Martijn Pieters Jan 06 '18 at 14:49
  • @Stack: and remove the self-documenting aspect, just because the line is getting a little long? – Martijn Pieters Jan 06 '18 at 14:49
  • Are you just asking how to format this, or how to refactor to avoid such a long list of parameters? Because in the second case, which is not Python-specific, you can introduce [*parameter objects*](http://wiki.c2.com/?ParameterObject) to group related parameters. Otherwise if you see `SpecificChampion("mana", "melee", 500, 20, 250, 20, 125, 50, 0.8, 65, 65, 320)` you always have to refer back to figure out what each parameter means. – jonrsharpe Jan 06 '18 at 14:50
  • the assignment is kinda same with `**kwargs` – Stack Jan 06 '18 at 14:51
  • @jonrsharpe I have to take each parameter and assign it to self.variable. Is there a way to do this to all parameters within a few lines? – ecoH Jan 06 '18 at 14:52
  • If that's what you're asking about, this is a dupe of e.g. https://stackoverflow.com/q/1389180/3001761 – jonrsharpe Jan 06 '18 at 14:54
  • @jonrsharpe yes thanks. I didn't find that question because i was not sure how to search it. – ecoH Jan 06 '18 at 14:55
  • I just Googled for *"python assign all init parameters to self"*. There are others, see also e.g. https://stackoverflow.com/q/3682137/3001761. Please don't put stuff like that in the title. – jonrsharpe Jan 06 '18 at 14:55
  • If you use positional arguments, why not just - https://eval.in/931591 – splash58 Jan 06 '18 at 15:36

0 Answers0