Making variable from a variable

  • Thread starter Polerio Babao Jr.II
  • Start date
P

Polerio Babao Jr.II

I don't know if this question is easy or not but one thing I can say
is
I haven't encountered this problem yet in my entire python life. I
just opened
this question to the public because I've solved this kind of problem
already
before in my PHP life long time ago. I just want to know if it's also
possible in Python.
Any idea? Thanks.

in php

<?

$a = 'b';
${$a} = 'c';
print "$a $b";

?>

output is

b c
 
D

Dave Benjamin

in php

<?

$a = 'b';
${$a} = 'c';
print "$a $b";

?>

output is

b c

Your PHP example can be accomplished with a dictionary, which is a better
way to go altogether in my opinion:
d = {}
d['a'] = 'b'
d[d['a']] = 'c'
print '%(a)s %(b)s' % d
b c

But if you absolutely must muck with the locals:

a = 'b'
locals()[a] = 'c'
print '%(a)s %(b)s' % locals()

Don't blame me if it's not backward/forward compatible, has unexpected
side-effects, or otherwise blows up in your face. I'm just the messenger.
Python 2.1, 2.2, and 2.3 tested.

<soapbox>
And don't program that way, anyway. It's just SILLY. (Believe me. I used to
write plenty of PHP.)
</soapbox>

Peace,
Dave
 
K

Karl Scalet

Polerio said:
I don't know if this question is easy or not but one thing I can say
is
I haven't encountered this problem yet in my entire python life. I
just opened
this question to the public because I've solved this kind of problem
already
before in my PHP life long time ago. I just want to know if it's also
possible in Python.
Any idea? Thanks.

in php

<?

$a = 'b';
${$a} = 'c';
print "$a $b";

?>

output is

b c

not quite as compact:

output is
b c

If/why someone want's to do it, is another question.

Karl
 
E

Erik Max Francis

Polerio Babao Jr.II said:
I don't know if this question is easy or not but one thing I can say
is
I haven't encountered this problem yet in my entire python life. I
just opened
this question to the public because I've solved this kind of problem
already
before in my PHP life long time ago. I just want to know if it's also
possible in Python.
Any idea? Thanks.

It is, with eval/exec. It's far more likely, though, that in Python a
better approach to this sort of problem would be to use a dictionary.
 
C

Cameron Laird

Your PHP example can be accomplished with a dictionary, which is a better
way to go altogether in my opinion: .
.
.
<soapbox>
And don't program that way, anyway. It's just SILLY. (Believe me. I used to
write plenty of PHP.)
</soapbox>
.
.
.
It's suboptimal *even in PHP*. It's a great puzzle
to my why leading PHPers continue to propagate this
unhelpful idiom.

I'll be more precise: there are few calls for appli-
cation programmers to give variables variable names.
"Systems programmers" writing frameworks, debugging
jigs, and such, certainly need the capability.
 
A

anton muhin

Polerio said:
I don't know if this question is easy or not but one thing I can say
is
I haven't encountered this problem yet in my entire python life. I
just opened
this question to the public because I've solved this kind of problem
already
before in my PHP life long time ago. I just want to know if it's also
possible in Python.
Any idea? Thanks.

in php

<?

$a = 'b';
${$a} = 'c';
print "$a $b";

?>

output is

b c

And kust my 2 cents (a safer way):
 
K

Kenneth Zhao

I don't know if this question is easy or not but one thing I can say
is
I haven't encountered this problem yet in my entire python life. I
just opened
this question to the public because I've solved this kind of problem
already
before in my PHP life long time ago. I just want to know if it's also
possible in Python.
Any idea? Thanks.

in php

<?

$a = 'b';
${$a} = 'c';
print "$a $b";

?>

output is

b c

Well, it's simple. See the following exercise:

bash-2.05b$ python
Python 2.2.3 (#1, Sep 11 2003, 08:34:13)
[GCC 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r1, propolice)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
a = 'b'
globals()[a] = 'c'
print a, b
b c

-- Ken
 

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
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top