0

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

zjm1126
  • 34,604
  • 53
  • 121
  • 166
  • Looks good. What's the problem? If you want to add anoher variable, you just edit it. What problem do you have? – S.Lott Mar 22 '11 at 02:17
  • 1
    I guess this is how dictionaries are created. If you think there are too many values, try to *refactor*. – miku Mar 22 '11 at 02:18
  • Where do all the variables come from? Arguments to a function? Globals? Somewhere else? – Baffe Boyois Mar 22 '11 at 02:19
  • @Baffe Boyois: I think they came from this question: http://stackoverflow.com/questions/5385815/the-best-way-to-define-these-variables-using-python – Greg Hewgill Mar 22 '11 at 02:21
  • they are the variables that i define . – zjm1126 Mar 22 '11 at 02:21
  • 4
    I would stop and consider why you are doing this. I can't help but think its not necessary. You are pretty much recreating `globals()`. Type that into your interpretter and see if you still want to do this. – jon_darkstar Mar 22 '11 at 02:24
  • @jon_darkstar, I would vote that comment up twice if I could. – senderle Mar 22 '11 at 02:25
  • the correct way of handling this depends on details not shown. You need to show us where you assign those variables. – Winston Ewert Mar 22 '11 at 02:27

4 Answers4

3

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.

jon_darkstar
  • 16,398
  • 7
  • 29
  • 37
  • oh haha i didnt see it above. yea i commented at first before i reconsidered and decided it was worth of an answer. thank you =P – jon_darkstar Mar 22 '11 at 02:29
2

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

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']
Mark Striemer
  • 95
  • 2
  • 9
  • I'm having trouble imagining how `locals()['attackUp']` is any better than simply `attackUp`, especially since it's slower and will suppress important optimisations in Python. – Greg Hewgill Mar 22 '11 at 02:34
  • I'm assuming he's wanting a way to lookup the variables based on the string version of their names so that he can dynamically call them, using `locals` would allow for that. – Mark Striemer Mar 22 '11 at 02:40
0

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.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99