Here's something that I've wished for a number of times so far: the ability to create a function (taking X) that creates X variables, even if they are all initialized to the same value. For example, f(3) would assign var1 = 0, var2 = 0, and var3 = 0.
My program runs an experiment, and I'd love for the number of sections to be a variable that I could change at will (so I could expand it later if I wanted). Each section requires a certain number of variables for keeping track of things like how many problems the participant solved correctly. So you can see how useful the above would be to me...
Anyway, I assume it can't be done. But are there other ways to deal with my problem? I'd love to find out.
hi.. I assume you can better handle this with using class. So I have a small piece of code, I assume i understand your problem.
class Exp:
"""This is an experiment class"""
def __init__(self, section, n):
temp = dict([i+1, 0] for i in range(n))
self.block = {section: temp}
def func(self, section):
for k, v in self.block[section].iteritems():
print k, v
print self.block[section][k]
if __name__ == '__main__':
obj = Exp('Qsection', 3)
print obj.func('Qsection')
lets say, you are conducting an experiment and it has several blocks, and each block has sections. And you want process the section's data.
The section's inputs vary. So, I created a class, which includes a dictionary(a hash).
The structure of the hash is
block => [section_name] => [v1:value, v2:value...]
so you can dynamically add number of keys to hash which is equal to creating that many number of variables.
Add functions to class as per your requirement.
Hope this helps.
kath.