[Tutor] Reference a variable from a string whose value is the name of the variable
Kent Johnson
kent37 at tds.net
Tue Jun 6 02:03:32 CEST 2006
Peter Jessop wrote:
> Thanks Andre
>
> I realise now I did not make it too clear.
> Basically the variable names are predictable but the values aren't.
>
> Maybe I should have expressed it like this
>
> f0_n = "arbitrayValue"
> f0_v ="another valule"
> f1_n="something else"
> f1_v="etc.."
> ...
>
> f100_n = "another value"
> f100_v = "nexvalue"
Generally the best way to do something like this is not to use named
variables at all, rather to use some kind of data structure to store the
values. In this case, you seem to have 101 pairs of values. Perhaps your
primary data structure should be the list of (fx_n, fx_v) values, rather
than trying to access the named variables.
Alternately, it looks like your data pairs are actually (name, value)
pairs. If so, and if the order of pairs is not important, you could
store them in a dictionary using the name as the key and the value as,
well, the value ;)
> I then wish to pass the list of 2ples as an argument to a function and
> thus I need the variable themselves and not the string values.
Hmm, I don't know what you mean by "the variable themselves". Do you
mean you want the called function to change the value of the variable?
That is hard to do in Python. But it's easy for a function to modify a
list or dictionary passed to it.
A little more context might be helpful here. Where do all these values
come from? What are you trying to do with them?
Kent
More information about the Tutor
mailing list