[Tutor] Variable help

Kent Johnson kent37 at tds.net
Mon Oct 9 20:51:30 CEST 2006


Tiago Saboga wrote:
> Hi!
> 
> I think your problem was solved, and that the list of lists is the better 
> approach indeed. But I'd like to add an answer to a direct question you 
> asked:
> 
> Em Domingo 08 Outubro 2006 07:47, Tom R. escreveu:
>> Basically I want to be able to integrate the value of one variable into
>> another variables name. 
> 
> I think you would like to do something like: 
> 
> =================================
> #!/usr/bin/env python
> # use of __get_attribute__() function
> class players(object):
> 	player1 = [2,4,8]
> 	player2 = [3,9,27]
> 	player3 = [4,16,64]
> 
> count = 2
> pl = players()
> actual_player_string = "player%s" % count
> 
> if pl.__getattribute__(actual_player_string)[2]==9:
> 	print 'It worked.'

Better to use the built-in getattr() function:
geattr(pl, actual_player_string)

> ===================================
> 
> I don't know how to do that if player{1,2,3} are not in a class, or if the 
> class is not instanciated. But somebody else can possibly show a way. 

There are ways to do it but really it's better not to.

Kent



More information about the Tutor mailing list