strings question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Faheem Mitha

    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.

  • William Park

    #2
    Re: strings question

    Faheem Mitha <faheem@email.u nc.edu> wrote:[color=blue]
    > 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.[/color]

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

    --
    William Park, Open Geometry Consulting, <opengeometry@y ahoo.ca>
    Linux solution for data processing and document management.

    Comment

    • Peter Hansen

      #3
      Re: strings question

      Faheem Mitha wrote:
      [color=blue]
      > 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.[/color]

      Assuming this is inside a method and not a function:

      def makeAttribute(s elf, 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

      Comment

      • Andrei

        #4
        Re: strings question

        Faheem Mitha wrote on Fri, 12 Mar 2004 18:03:05 GMT:
        [color=blue]
        > 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.[/color]

        You could use exec (potentially dangerous if not all code being exec-ed is
        under your control):
        [color=blue][color=green][color=darkred]
        >>> f = 'foo'
        >>> b = 'bar'
        >>> exec '%s = b' % f
        >>> foo[/color][/color][/color]
        'bar'

        You could also manipulate the globals or locals dictionary:
        [color=blue][color=green][color=darkred]
        >>> globals()[f] = b*2
        >>> foo[/color][/color][/color]
        'barbar'[color=blue][color=green][color=darkred]
        >>> locals()[f] = b*3
        >>> foo[/color][/color][/color]
        'barbarbar'

        --
        Yours,

        Andrei

        =====
        Real contact info (decode with rot13):
        cebwrpg5@jnanqb b.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
        gur yvfg, fb gurer'f ab arrq gb PP.

        Comment

        • Andrew Koenig

          #5
          Re: strings question


          "Faheem Mitha" <faheem@email.u nc.edu> wrote in message
          news:slrnc53uq9 .2lu.faheem@Chr estomanci.home. earth...
          [color=blue]
          > 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?[/color]

          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.


          Comment

          • Faheem Mitha

            #6
            Re: strings question

            On Sat, 13 Mar 2004 05:26:19 GMT, Andrew Koenig <ark@acm.org> wrote:[color=blue]
            >
            > "Faheem Mitha" <faheem@email.u nc.edu> wrote in message
            > news:slrnc53uq9 .2lu.faheem@Chr estomanci.home. earth...
            >[color=green]
            >> 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?[/color]
            >
            > 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.[/color]

            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.

            Comment

            Working...