Python Forum

Full Version: I need help with loop one function and creating variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my code that is working
*****************************************************
names = ['xx','yy','cc']

for name in names:
    exec("%s_mar = []" % name)
    some code...
    else:
       pass

xx_var = countAll(xx_mar)
yy_var = countAll(yy_mar)
cc_var = countAll(cc_mar)
***********************************************************
I need help with loop one function and creating variables - code below doesn't work

for name in names:
    eval("%s_var)" % name) = countAll('{0}_mar'.format(name))
    else:
       pass
http://stupidpythonideas.blogspot.com/20...reate.html
don't create names dynamically, use dict instead
thx for replay
also need help with this

time = countAll(x_mar)
#this works
name = 'x'
time = countAll('{0}_mar'.format(name))
#this doesn't work
You are not listening and keep trying to create variables names dynamically. Almost always and certainly in this case it's a bad idea.
Please put your question in broader context and elaborate what your goal is, so we can understand and try to help you better