1

I have recently undertook converting all of my older batch files towards running on Python 3.6 to allow for them to run on multiple OS's. During this process I have hit a wall, my question is how to best convert these following batch commands to something capable of being run in Python. I have looked around extensively and it has all been for naught. I am open to suggestions.

set number=1
set var%number%=[some value here]

I have already tried things similar to this in Python:

number = 1
("var"+str(number)) = [some value here]

As well as this to try to get the interpreter to process the variable I'm going to assign to done before I attempt assignment.

number = 1
var = ("var"+str(number))
(var) = [some value here]

The reason why I am trying to convert the first method is so that the program could initially set a changing value to 'var1' then 'var2' then 'var3' etc. Is this one of those things that is unique to batch files and not languages such as Python?

Ahmouse
  • 195
  • 1
  • 12
Peter
  • 13
  • 3
  • `var` should be a proper list or dictionary; then you could just index it and assign values to its individual elements. – Matteo Italia Jun 17 '18 at 05:09

2 Answers2

0

If you're trying to change the value of number then set the value by number = 1 And change it again by doing the same number = 6

Edit: I see what you're trying to do, it is better not to do this in Python and you should try to avoid it for multiple reasons, but anyways here's an example of using exec (assuming number equals to 1):

exec("var"+str(number)+"=0")

Which would run

var1=0
Ahmouse
  • 195
  • 1
  • 12
0

Like this answer says, you could just do:

numbers = [1, 2, 3]
for n in numbers:
    exec('var_%d = [20]' % n)
print(var_1) # prints [20]
print(var_2) # prints [20]    
print(var_3) # prints [20]

However, there are arguments against the usage of exec in that answer, so you should consider a dictionary. You can define as many keys (var1, var2, etc...) as you need.

if __name__ == '__main__':
    v = {}
    numbers = [1, 2, 3]
    for n in numbers:
        v['var' + str(n)] = 10 # initial value 10
    print(v['var1']) # 10
    print(v['var2']) # 10
    print(v['var3']) # 10

    v['var2'] = 20 # update value to 20
    print(v['var2']) # 20

Or if you need more customization, maybe consider defining your own class?

class MyVar(object):
    def set(self, number, value):
        setattr(self, 'var' + str(number), value)

if __name__ == '__main__':
    number = 1
    v = MyVar()
    v.set(number , 20) 
    print(v.var1) # prints 20
    v.set(number , 40)
    print(v.var1) # now prints 40
    v.var1 = 50
    print(v.var1) # now prints 50

It's hard to recommend anything else without knowing more about what you want to do.

Tomas Farias
  • 1,293
  • 1
  • 14
  • 18
  • I couldn't really include a ton of detail as to what I want to do, because I have a whole bunch of varying applications for this question, and to be honest, I didn't even think about digging into defining classes and such. Thanks for your recommendations. – Peter Jun 17 '18 at 22:34