简体   繁体   中英

best way to create this dict in python

this is my code :

vars_ = {
                'attackUp':attackUp,'defenceUp':defenceUp,'magicUp':magicUp,'attType':attType,'weightDown':weightDown,
                'accAttackSword':accAttackSword,'accAttackSaber':accAttackSaber,'accAttackAx':accAttackAx,
                'accAttackHammer':accAttackHammer,'accAttackSpear':accAttackSpear,'accAttackFight':accAttackFight,
                'accAttackBow':accAttackBow,'accAttackMagicGun':accAttackMagicGun,'accAttackMagic':accAttackMagic,
                'mStrInstrument':mStrInstrument,'mStrCharms':mStrCharms,'accDefencePhy':accDefencePhy,
                'accDefenceMag':accDefenceMag,'accWeight':accWeight,'bookTurn':bookTurn,'bookAttackPhy':bookAttackPhy,
                'bookAttackMag':bookAttackMag,'bookStrInstrument':bookStrInstrument,'bookStrCharms':bookStrCharms,
                'bookDefencePhy':bookDefencePhy,'bookDefenceMag':bookDefenceMag,'bookWeight':bookWeight,'name':name,
                'plvl':plvl,'str':str,'ski':ski,'mag':mag,'spd':spd,'locX':locX,'locY':locY,'wName':wName,
                'wAttack':wAttack,'wDefence':wDefence,'wWeight':wWeight,'wType':wType,'target':target,'title':title,
                'uname':uname,'cUrl':cUrl,'mbCnt':mbCnt
                }

oh my god , I spent a lot of time on this work , and maybe have more Variable to be added later ,

any easy way to do this ,

thanks

I would stop and consider why you are doing this. I can't help but think its not necessary.

Even if you decide this is necessary (which i doubt) - You are pretty much recreating globals() . Type that into your interpretter and see if you still want to do this.

Organize it further like senderle suggested in your other post. And maybe post a broader question with help for organizing your project.

The first thing I would do is reformat that dictionary so there is one entry per line:

vars_ = {
    'attackUp'  : attackUp,
    'defenceUp' : defenceUp,
    'magicUp'   : magicUp,
    'attType'   : attType,
    'weightDown': weightDown,
    # and so on
}

I have also lined up the columns so the whole list reads more easily.

You could make an array of variable names and pull them out of the locals dictionary.

x, y, z = 5, 10, 20
l = locals()
d = {}
for v in ['x', 'y', 'z']:
    d[v] = l[v]
# d = {'y': 10, 'x': 5, 'z': 20}

locals might work on it's own too if you're just wanting to look it up as a string.

attUp = locals()['attackUp']

I totally agree with @miku - look at how you are using the values and seriously refactor.

For example, a Character has Attributes (physical_attack, physical_defence, magic_attack, magic_defence, weight, speed) and Items; Weapons are Items, Swords and Axes and Spears and Bows are Weapons, a Saber is a Sword. Unarmed is a special default Weapon. Charms are Items, but apparently Books and StringedInstruments are Weapons?? Items have Attributes which are added to a Character's Attributes while equipped. Character also has level, location, target, and an accuracy rating for each weapon type (can a Weapon have an accuracy-modifier?).

If you break it down into a class hierarchy this way, it should be much easier to keep track of what you are doing.

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