0

I have a ton of variables that I need to create and reference, for example diceOutCome2, diceOutCome4... diceOutCome12, diceOutComeHornHigh, diceOutComeHornFive, etc and I constanctly need to reference them so I wish to store them as global variables instead of passing them around as parameters.

Most importantly, I am also trying Not to reference them with index numbers but instead with names or property like counter.diceOutCome2... 2 here is not the number 2 outcome but outcome with dice rolled 1 and 1, four would store dice total with 2,2, 1,3 information etc.

What's the most efficient way and the correct syntax to create them?

def setGlobalVariables():
    counter=[]
    for i in range (0, 12):
        global counter["count_"+i]=0
FongYu
  • 767
  • 4
  • 15
  • 24
  • 6
    Just don't. Make lists, or perhaps dicts, and use indices. –  Sep 02 '12 at 02:20
  • 1
    Who says you have a ton of variables that you need to create? That sounds like a lot of work to automate, debug, etc. Could some additional planning, before coding, reduce the necessary effort and the complexity? – Paul Sep 02 '12 at 02:21
  • possible duplicate of [Python: Using vars() to assign a string to a variable](http://stackoverflow.com/questions/2320945/python-using-vars-to-assign-a-string-to-a-variable) – BrenBarn Sep 02 '12 at 02:27
  • 1
    @Paul - for the OP, I think the issue is not so much insufficient planning as it is insufficient education. Naming variables with trailing numeric "indices" is a common tactic among new programmers - in fact some tutorials "teach" it in early lessons in order to lead the tutoree down to the inevitable dead end, in order to present the topic of indexing into collections as a tidy way out of such a mess. I think user1518600 is just jumping too quickly into a complex application before learning about the basic programming tools and techniques, after which his/her planning will be better informed. – PaulMcG Sep 02 '12 at 02:58

1 Answers1

3

For properties like "diceOutCome2", you are actually better off using a list with index numbers, like counter.diceOutCome[2]. This allows you to easily iterate over all the list of diceOutcomes, or to refer to the outcome before the 5th dice outcome as counter.diceOutCome[n-1] when n is 5.

Horn bets can be computed as methods on an object, either the counter object that you referenced before (counter.highHorn(5) could evaluate the winning or losing of a high horn bet on counter.diceOutcome[5]), or on the object used to represent the die roll outcome, a list of which is kept in diceOutCome, as in counter.diceOutcome[5].highHorn(). Or HighHorn could be an subclass of Bet; an instance of HighHorn is constructed using a DiceOutcome, and HighHorn would implement a wins() method, defined in abstract on Bet, to be implemented in subclasses - evaluating it as HighHorn(counter.diceOutCome[5]).wins(). Since the diceOutcomes are all attached to counter, then this might be a logical item to pass around to related methods, or a logical item on which to define those methods.

In general, if you start thinking about defining variable names with trailing numeric digits, for instance that something1, something2, something3 are going to be of use in your program, you must immediately stop and replace with a list called somethings, and access them using list indices like somethings[2].

(Note - For ease of communication, I am using 1-based indexing to correspond to the descriptions - in actuality, indexes to the items in a sequence are 0-based, hence the 5th item would really be referenced as sequence[4], the item before it is sequence[4-1], the 1st item being found atsequence[0]`. The point is, this is the case where indexes make sense, not numeric suffixes on variable names.)

PaulMcG
  • 62,419
  • 16
  • 94
  • 130