943,671 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 3261
  • Python RSS
Mar 13th, 2007
0

Function for assigning variables

Expand Post »
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.
Similar Threads
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Mar 13th, 2007
1

Re: Function for assigning variables

Click to Expand / Collapse  Quote originally posted by aot ...
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.
If I understand you correctly, there are several ways:

# 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
>>>
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Mar 14th, 2007
1

Re: Function for assigning variables

Click to Expand / Collapse  Quote originally posted by aot ...
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.

Python Syntax (Toggle Plain Text)
  1. class Exp:
  2. """This is an experiment class"""
  3. def __init__(self, section, n):
  4. temp = dict([i+1, 0] for i in range(n))
  5. self.block = {section: temp}
  6.  
  7. def func(self, section):
  8. for k, v in self.block[section].iteritems():
  9. print k, v
  10. print self.block[section][k]
  11.  
  12. if __name__ == '__main__':
  13. obj = Exp('Qsection', 3)
  14. 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.
Reputation Points: 19
Solved Threads: 34
Posting Whiz in Training
katharnakh is offline Offline
237 posts
since Jan 2006
Mar 14th, 2007
0

Re: Function for assigning variables

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

Python Syntax (Toggle Plain Text)
  1. def f(x):
  2. 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)
  1. class Section(object):
  2. def __init__(self):
  3. self.score = 0
  4. self.name = ""
  5. ... # etc.
  6.  
  7. def f(x):
  8. return [Section() for i in range(x)]

Now, f(x) returns x number of Section objects ready for use.

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 15th, 2007
0

Re: Function for assigning variables

Click to Expand / Collapse  Quote originally posted by jrcagle ...
Hey ... weird... I thought I had posted here earlier.
hey, I din't aware of your post.


kath.
Reputation Points: 19
Solved Threads: 34
Posting Whiz in Training
katharnakh is offline Offline
237 posts
since Jan 2006
Mar 15th, 2007
0

Re: Function for assigning variables

Oh, sorry ... wasn't complaining about your post, which was great. I was just surprised to find myself reading the same question that I thought I'd replied to earlier... :lol:
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 16th, 2007
0

Re: Function for assigning variables

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 exec function in order to assign some features to the different trial blocks. We'll see how well it goes.
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python c interface problems
Next Thread in Python Forum Timeline: frame by frame animation in pygame





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC