Python Forum
I need help with loop one function and creating variables - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I need help with loop one function and creating variables (/thread-15594.html)



I need help with loop one function and creating variables - don - Jan-23-2019

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



RE: I need help with loop one function and creating variables - buran - Jan-23-2019

http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html
don't create names dynamically, use dict instead


RE: I need help with loop one function and creating variables - don - Jan-24-2019

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



RE: I need help with loop one function and creating variables - buran - Jan-24-2019

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