[Tutor] from string to variable name

Luke Paireepinart rabidpoobear at gmail.com
Thu Oct 5 15:49:14 CEST 2006

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

More information about the Tutor mailing list