variable variables

Steven D. Majewski sdm7g at virginia.edu
Mon Dec 11 14:08:59 EST 2000


On Mon, 11 Dec 2000 pyth at relax.dk wrote:
> 
> Sorry Mikael, for using your time... I should have been more specific...
> What i was thinking of, was something like this:
> 
> test1 = 'test text 1';
> test2 = 'test text 2';
> 
> var1 = 1
> var2 = 2
> 
> print test{var1};
> 
> This should print: 'test text 1'
> This example doesn't work, but is close how you would do it in php :o)
> 

Why not just use an array:

test = [ 'test text 1', 'test text 2' ]

for var in 0,1: 
	print test[var]


Or if you want to index by something other than an int, a dictionary:

test = { 'one':'test text 1' , 'two':'test text 2' }
for var in 'one','two':
	print test[var]


-- Steve Majewski <sdm7g at Virginia.EDU>






More information about the Python-list mailing list