Newbie Question

U

Uday

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
 
S

Steve Holden

[posted and mailed]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,210
Messages
2,571,091
Members
47,691
Latest member
Jenny-jane

Latest Threads

Top