469,610 Members | 1,768 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

strings question

Dear People,

If I am given two strings, named foo and bar, what is the most elegant
way to create a string object whose name is the value of foo and whose
value is the value of bar? I have done some searching + looking in my
introductory textbook "Learning Python" but have not come up with an
answer yet.

Thanks. Faheem.

Jul 18 '05 #1
5 1046
Faheem Mitha <fa****@email.unc.edu> wrote:
Dear People,

If I am given two strings, named foo and bar, what is the most elegant
way to create a string object whose name is the value of foo and whose
value is the value of bar? I have done some searching + looking in my
introductory textbook "Learning Python" but have not come up with an
answer yet.

Thanks. Faheem.


Read the doc on 'eval' and 'exec'.

--
William Park, Open Geometry Consulting, <op**********@yahoo.ca>
Linux solution for data processing and document management.
Jul 18 '05 #2
Faheem Mitha wrote:
Dear People,

If I am given two strings, named foo and bar, what is the most elegant
way to create a string object whose name is the value of foo and whose
value is the value of bar? I have done some searching + looking in my
introductory textbook "Learning Python" but have not come up with an
answer yet.


Assuming this is inside a method and not a function:

def makeAttribute(self, foo, bar):
setattr(self, foo, bar)

will do the trick.

If you need it in a function, use globals() as a dictionary, but note
that you are getting a global, not a local. You can't do it effectively
if what you want is a local.

-Peter
Jul 18 '05 #3
Faheem Mitha wrote on Fri, 12 Mar 2004 18:03:05 GMT:
If I am given two strings, named foo and bar, what is the most elegant
way to create a string object whose name is the value of foo and whose
value is the value of bar? I have done some searching + looking in my
introductory textbook "Learning Python" but have not come up with an
answer yet.


You could use exec (potentially dangerous if not all code being exec-ed is
under your control):
f = 'foo'
b = 'bar'
exec '%s = b' % f
foo 'bar'

You could also manipulate the globals or locals dictionary:
globals()[f] = b*2
foo 'barbar' locals()[f] = b*3
foo

'barbarbar'

--
Yours,

Andrei

=====
Real contact info (decode with rot13):
ce******@jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
Jul 18 '05 #4

"Faheem Mitha" <fa****@email.unc.edu> wrote in message
news:sl*******************@Chrestomanci.home.earth ...
If I am given two strings, named foo and bar, what is the most elegant
way to create a string object whose name is the value of foo and whose
value is the value of bar?


The most elegant way might be to avoid altogether the notion of computing
variable names and use a dict instead. It is unlikely that you really want
to be able to comput a variable name on the fly, because what happens if the
resulting variable happens to clash with a name that you are already using?

If you could say a little more about your application, perhaps we could tell
you more.
Jul 18 '05 #5
On Sat, 13 Mar 2004 05:26:19 GMT, Andrew Koenig <ar*@acm.org> wrote:

"Faheem Mitha" <fa****@email.unc.edu> wrote in message
news:sl*******************@Chrestomanci.home.earth ...
If I am given two strings, named foo and bar, what is the most elegant
way to create a string object whose name is the value of foo and whose
value is the value of bar?


The most elegant way might be to avoid altogether the notion of computing
variable names and use a dict instead. It is unlikely that you really want
to be able to comput a variable name on the fly, because what happens if the
resulting variable happens to clash with a name that you are already using?

If you could say a little more about your application, perhaps we could tell
you more.


Hi,

Yes, someone else also suggested a dictionary, and I agree it is the
best way to go. Potential name clashes had not occurred to me till you
mentioned it, though.

I'm trying to produce a list of DNA sequences for further manipulation
with the names and the values of the sequences read from a file into
strings, and so in this case, the value of foo would be the name of
the sequence, and the value of bar would be the value of the sequence.

However, I'll look at what other people have suggested. I am new to
python and so don't even understand the answers, but will read up on
exec etc.

I own one of your books, by the way (Accelerated C++). :-)

Thanks to everyone who replied.

Faheem.
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jean-Fran?ois Lacrampe | last post: by
16 posts views Thread by Paul Prescod | last post: by
9 posts views Thread by Rafi Kfir | last post: by
7 posts views Thread by drewnoakes | last post: by
19 posts views Thread by pkirk25 | last post: by
9 posts views Thread by Omatase | last post: by
95 posts views Thread by hstagni | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.