Newbie Question
Steve Holden
steve at holdenweb.com
Tue Nov 2 20:18:41 EST 2004
[posted and mailed]
Uday wrote:
> Hi,
> I would like to know how can I create and assign variables by
> de-referencing the values in other variable in Python.
>
> I can illustrate what I mean by an example of perl
>
> $a = "task_id";
> $i = "1";
> $c = $a."_".$i;
> $$c = 2;
> print "$task_id_1\n";
> # Gives the answer as 2
>
> My problem is that I need to dynamically generate a unique
> variable within a for loop in Python. The name of the variable
> should be appended by the index of the loop.
>
> In the above example if $i were an array index, then as we
> traverse through the array, a unique variable is defined.
>
> Is there any way to do it in Python??
>
> I am not in python.users mailing list, please reply to me directly.
>
> Thanks,
> Uday
It's bad enough doing that in Perl, but in Python you should definitely
use a dictionary (in Perl read "hash") for applications like this. What
do you see as the advantage of your code over using a hash?
regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
More information about the Python-list
mailing list