简体   繁体   中英

Can I add a dictionary as a class attribute in Python?

I'm working on a DND project that completely creates a character for you. I want to make it so if an ability score is a certain number or within a certain range of numbers, the program will adjust the ability score.

def __init__(self, name, race, sub_race, char_class, size='Small', speed=0, strength=0, dexterity=0, constitution=0,
                 intelligence=0, wisdom=0, charisma=0):
        self.name = name
        self.race = race
        self.sub_race = sub_race
        self.char_class = char_class
        self.size = size
        self.speed = speed
        self.strength = strength
        self.dexterity = dexterity
        self.constitution = constitution
        self.intelligence = intelligence
        self.wisdom = wisdom
        self.charisma = charisma

I'd like to make it so the abilities (speed, strength, dexterity, constitution, intelligence, wisdom, charisma) are in a dictionary within the class. This way I can update each individual ability score.

    def adjust_scores(self):
        if (self.speed or self.strength or self.dexterity or self.constitution or self.intelligence or self.wisdom or
            self.charisma) == 1:

Is there a way to make these scores into a dictionary and possibly iterate through each key/value pair and update their values?

The solution is actually quite simple. You can store a dictionary instead of attributes, like you are asking in the question, as shown below:

def __init__(self, name, race, sub_race, char_class, size='Small', speed=0, strength=0, dexterity=0, constitution=0,
                 intelligence=0, wisdom=0, charisma=0):
        self.attributes = {}
        self.attributes['name'] = name
        self.attributes['race'] = race
        self.attributes['sub_race'] = sub_race
        self.attributes['char_class'] = char_class
        self.attributes['size'] = size
        self.attributes['speed'] = speed
        self.attributes['strength'] = strength
        self.attributes['dexterity'] = dexterity
        self.attributes['constitution'] = constitution
        self.attributes['intelligence'] = intelligence
        self.attributes['wisdom'] = wisdom
        self.attributes['charisma'] = charisma

If you want to retain your specific attributes as well as the dictionary, you can have the best of both worlds by having the constructor be:

def __init__(self, name, race, sub_race, char_class, size='Small', speed=0, strength=0, dexterity=0, constitution=0,
                 intelligence=0, wisdom=0, charisma=0):
        self.attributes = {}
        self.attributes['name'] = name
        self.attributes['race'] = race
        self.attributes['sub_race'] = sub_race
        self.attributes['char_class'] = char_class
        self.attributes['size'] = size
        self.attributes['speed'] = speed
        self.attributes['strength'] = strength
        self.attributes['dexterity'] = dexterity
        self.attributes['constitution'] = constitution
        self.attributes['intelligence'] = intelligence
        self.attributes['wisdom'] = wisdom
        self.attributes['charisma'] = charisma
        self.name = name
        self.race = race
        self.sub_race = sub_race
        self.char_class = char_class
        self.size = size
        self.speed = speed
        self.strength = strength
        self.dexterity = dexterity
        self.constitution = constitution
        self.intelligence = intelligence
        self.wisdom = wisdom
        self.charisma = charisma

and either:

1 - when changing one of them, change the other 2 - have a special setter function for each attribute, which also changes the dict. Ex:

def setName(self, newName):
    self.name = newName
    self.attributes['name'] = newName

Edit: Sorry for my incorrect remarks about setters previously. As pointed out in comments, setters and the second method are generally a bad practice in Python (this doesn't apply to other languages such as Java, with specifications such as private ).

However, this is by no means stopping you from using that method. If I understand your use-case correctly, then you shouldn't be extremely nitpicky at conventions, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM