[Tutor] from string to variable name

2006-10-05 Thread frank h.
hello, i have a string variable that contains a name that I want to use as a variablenameputting aside questions of why I would like to do that - i this possible at all?so I want to assign a value to a variable whos name is available only as a string to me.
that variable does not exist yet in the local namespace.from: t = myvarto: myvar = 3is this possible? something like setattr?thanks for any insight you might have-frank

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] from string to variable name

2006-10-05 Thread Luke Paireepinart
frank h. wrote:
 hello, i have a string variable that  contains a name that I want to 
 use as a variablename
 putting aside questions of why I would like to do that - i this 
 possible at all?

 so I want to assign a value to a variable whos name is available only 
 as a string to me.
 that variable does not exist yet in the local namespace.

 from: t = myvar
 to: myvar = 3

 is this possible? something like setattr?
 thanks for any insight you might have
I think the point of dictionaries is to get this same basic 
functionality without polluting the namespaces.
You won't let us ask you 'why' you want to do this,
but I'll ask you: Why don't you want to use a dictionary?
Do you want to know if it's _Possible_ just so you'll know,
or do you actually want to use this for something?
If you just want to know if it's possible, I believe it is.
But consider:
if you don't know the variable name until runtime,
how are you going to refer to the variable later in your code?
It would be, insofar as I can tell, useless to do this.
 -frank
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] from string to variable name

2006-10-05 Thread Jonathon Sisson
By string variable that contains a name that I want to use as a
variablename do you mean something like this:

myString = rotationalSpeed
rotationalSpeed = 4500

??

In Python a dictionary is an excellent solution to this problem.  The
only other way to accomplish this (to my knowledge) is in PHP (not
trying to steer you away from Python, just giving some info):

var myString = rotationalSpeed;
$$myString = 4500;
echo $rotationalSpeed;

results in 4500.  This is called variable variables in PHP and it can
get hairy if done in a sloppy manner.

I'm going to make the same recommendation that Luke did.  Dictionaries
are powerful structures.  I recently wrote a Python script to parse an
English dictionary file and build Markov models out of the words
contained in the file (no, it wasn't for school, or work, or
anything...I was just really, really bored).  Rather than declaring an
int to hold the frequency information for every letter, digram, and
trigram possible (a total of 18,278 declarations), I used the letter,
digram, or trigram as a key into a dictionary.  Now I can do simple
lookups by individual letters, digrams, or trigrams and see the
frequency information without having to reference thousands of
variables, and as an added side effect, only the letters, digrams, and
trigrams that actually occur require storage.

Jonathon


Luke Paireepinart wrote:
 frank h. wrote:
 hello, i have a string variable that  contains a name that I want to 
 use as a variablename
 putting aside questions of why I would like to do that - i this 
 possible at all?

 so I want to assign a value to a variable whos name is available only 
 as a string to me.
 that variable does not exist yet in the local namespace.

 from: t = myvar
 to: myvar = 3

 is this possible? something like setattr?
 thanks for any insight you might have
 I think the point of dictionaries is to get this same basic 
 functionality without polluting the namespaces.
 You won't let us ask you 'why' you want to do this,
 but I'll ask you: Why don't you want to use a dictionary?
 Do you want to know if it's _Possible_ just so you'll know,
 or do you actually want to use this for something?
 If you just want to know if it's possible, I believe it is.
 But consider:
 if you don't know the variable name until runtime,
 how are you going to refer to the variable later in your code?
 It would be, insofar as I can tell, useless to do this.
 -frank
 -Luke
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] from string to variable name

2006-10-05 Thread wesley chun
On 10/5/06, frank h. [EMAIL PROTECTED] wrote:
 hello, i have a string variable that  contains a name that I want to use as
 a variablename


setattr() won't work because that's only for objects which have
attributes.  you're talking about creating a (global or local)
variable.  i will also recommend you use a dictionary too... it's
flexible and powerful.

but if you *have* to do it, to paraphrase jonathon's example:

 myString = 'rotationalSpeed'
 exec '%s = 4500' % myString
 print rotationalSpeed
4500

it's ugly but works. this example is more useful if you're planning on
dynamically-generated lots of Python code to execute, not just a
single assignment like this.

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor