| | |
Function for assigning variables
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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,
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.
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.
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
•
•
•
•
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 assignvar1 = 0,var2 = 0, andvar3 = 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.
# Update the local namespace
>>> for i in range(25):
... exec "%s = %d" % ('var'+str(i), 0) in None
# Update the global namespace
>>> dd = {}
>>> for i in range(25):
... dd['var'+str(i)] = 0
>>> globals.update(dd)
# Update the local namespace with dictionary dd
>>> for key in dd:
... exec "%s = %s" % (key, repr(dd[key])) in None
...
>>>
# Update a module namespace with dd
>>> import P3D
>>> P3D.__dict__.update(dd)
>>> var0
0
>>> var1
0
>>> var24
0
>>>
•
•
•
•
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 assignvar1 = 0,var2 = 0, andvar3 = 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.
Python Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Hey ... weird... I thought I had posted here earlier. Anyways, assuming that you are representing each section with a single integer, you might do well to simply maintain a list of sessions. In that case, your function would be
If it turns out you need more info for your sections, you could create a Section() class:
Now, f(x) returns x number of Section objects ready for use.
Jeff
Python Syntax (Toggle Plain Text)
def f(x): return [0]*x
If it turns out you need more info for your sections, you could create a Section() class:
Python Syntax (Toggle Plain Text)
class Section(object): def __init__(self): self.score = 0 self.name = "" ... # etc. def f(x): return [Section() for i in range(x)]
Now, f(x) returns x number of Section objects ready for use.
Jeff
Wow, a big thanks to all of you! Now to compile this information in the ol' brain and see if I can improve my experiment program...
I still honestly have a lot to learn about structuring my whole program. Right now I have a couple of different classes based on the type of page that the participant will be viewing (an instructions page, a trial page, or a questionnaire page), and I've got a multiway branch set up that directs them to the appropriate type based on what section of the experiment they're in... and the section is just an int that increments as they progress along. I suppose it's not too bad overall, for a first try anyway.
Anyway, my plan right now is to use solsteel's suggestion of the
I still honestly have a lot to learn about structuring my whole program. Right now I have a couple of different classes based on the type of page that the participant will be viewing (an instructions page, a trial page, or a questionnaire page), and I've got a multiway branch set up that directs them to the appropriate type based on what section of the experiment they're in... and the section is just an int that increments as they progress along. I suppose it's not too bad overall, for a first try anyway.
Anyway, my plan right now is to use solsteel's suggestion of the
exec function in order to assign some features to the different trial blocks. We'll see how well it goes. ![]() |
Similar Threads
- Deleting Characters Function (C)
- Calling C function from MATLAB (C)
- can anyone assiat in getting my function to work? (C)
- Problem with php5 variables (PHP)
Other Threads in the Python Forum
- Previous Thread: python c interface problems
- Next Thread: frame by frame animation in pygame
Views: 2506 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt apache approximation argv array beginner binary book calculator change cipher clear code converter countpasswordentry cturtle dictionaries dictionary drive dynamic examples excel file float format ftp function gui hints homework import inches input java keyboard library line linux list lists loop maze mouse mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive remote script scrolledtext search session signal singleton socket ssh string strings strip table terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable verify vigenere windows wordgame wxpython xlwt





