Store variable name in another variable

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

    Store variable name in another variable

    I need to store a list of variable names in a dictionary or list. I
    then later need to retrieve the names of the variables and get the
    values from the named variables. The named variables will already have
    been created and given a value.

    I hope thats clear!!!

    How can I do this?

  • Larry Bates

    #2
    Re: Store variable name in another variable

    loial wrote:
    I need to store a list of variable names in a dictionary or list. I
    then later need to retrieve the names of the variables and get the
    values from the named variables. The named variables will already have
    been created and given a value.
    >
    I hope thats clear!!!
    >
    How can I do this?
    >
    Use a dictionary. Example:

    var1=10
    var2="abc"
    var2=1.2

    vardict['var1']=var1
    vardict['var2']=var2
    vardict['var3']=var3

    print vardict['var1']
    print vardict['var2']
    print vardict['var3']

    -Larry

    Comment

    • Laurent Pointal

      #3
      Re: Store variable name in another variable

      loial a écrit :
      I need to store a list of variable names in a dictionary or list. I
      then later need to retrieve the names of the variables and get the
      values from the named variables. The named variables will already have
      been created and given a value.
      "Named variables will already have been created"... in what namespace ?

      Store a list of names -dict/list (see tutorial)

      Then,
      use namespace.name
      or getattr(namespa ce,"name")
      or locals()["name"]
      or globals()["name"]


      Comment

      • Cameron Laird

        #4
        Re: Store variable name in another variable

        In article <f0q89v$uge$1@n ews2.u-psud.fr>,
        Laurent Pointal <laurent.pointa l@limsi.frwrote :
        >loial a écrit :
        >I need to store a list of variable names in a dictionary or list. I
        >then later need to retrieve the names of the variables and get the
        >values from the named variables. The named variables will already have
        >been created and given a value.
        >
        >"Named variables will already have been created"... in what namespace ?
        >
        >Store a list of names -dict/list (see tutorial)
        >
        >Then,
        >use namespace.name
        >or getattr(namespa ce,"name")
        >or locals()["name"]
        >or globals()["name"]
        >
        >
        admin, I want to be sure you understand the advice you've been given.
        Yes, it is possible to "double dereference" through named variables;
        HOWEVER, it is essentially *never* advantageous to do so in application
        programming with Python (nor is it in Perl and PHP, despite what many
        senior people there teach). Use a dictionary, perhaps one with
        multi-dimensional keys.

        Comment

        • Laurent Pointal

          #5
          Re: Store variable name in another variable

          Cameron Laird wrote:
          In article <f0q89v$uge$1@n ews2.u-psud.fr>,
          Laurent Pointal <laurent.pointa l@limsi.frwrote :
          >>loial a écrit :
          >>I need to store a list of variable names in a dictionary or list. I
          >>then later need to retrieve the names of the variables and get the
          >>values from the named variables. The named variables will already have
          >>been created and given a value.
          >>
          >>"Named variables will already have been created"... in what namespace ?
          >>
          >>Store a list of names -dict/list (see tutorial)
          >>
          >>Then,
          >>use namespace.name
          >>or getattr(namespa ce,"name")
          >>or locals()["name"]
          >>or globals()["name"]
          >>
          >>
          >
          admin, I want to be sure you understand the advice you've been given.
          Yes, it is possible to "double dereference" through named variables;
          HOWEVER, it is essentially *never* advantageous to do so in application
          programming with Python (nor is it in Perl and PHP, despite what many
          senior people there teach). Use a dictionary, perhaps one with
          multi-dimensional keys.
          Yes, I understand. I just reply to OP question not searching the reason why
          he wants to manage *variables* this way (he talk about dict, so I hope he
          know that they can be used to map names to values).
          So, this is not an advice, just technical ways related to the question as it
          was written.

          And yes, personnally i use dictionnaries for such purpose.

          A+

          Laurent.

          Comment

          • loial

            #6
            Re: Store variable name in another variable

            OK, I have it working with dictionaries.

            However I have another scenerio :

            I am reading a file containing records like the following :

            <IMPOSITION_DOC UMENT>
            <WFBAN_PAGE>
            <WFBAN_START_DE TAILS name=\""MYVARIA BLE"\">
            ...
            ...


            I need to substitute MYVARIABLE with the current value of MYVARIABLE
            in my python script and write the file out again.

            The file may contain many more lines and many substitution values on
            any line

            Assuming that MYVARIABLE is currently set to JOHN then the output
            would be

            <IMPOSITION_DOC UMENT>
            <WFBAN_PAGE>
            <WFBAN_START_DE TAILS name="JOHN">

            Can this be done in Python? Amending the way the variable names are
            distinguished in the incoming file is possible if that would help.







            On 26 Apr, 22:02, Laurent Pointal <laurent.poin.. .@wanadoo.frwro te:
            Cameron Laird wrote:
            In article <f0q89v$ug...@n ews2.u-psud.fr>,
            Laurent Pointal <laurent.poin.. .@limsi.frwrote :
            >loial a ?it :
            >I need to store a list ofvariablenames in a dictionary or list. I
            >then later need to retrieve the names of the variables and get the
            >values from the named variables. The named variables will already have
            >been created and given a value.
            >
            >"Named variables will already have been created"... in what namespace ?
            >
            >Store a list of names -dict/list (see tutorial)
            >
            >Then,
            >use namespace.name
            >or getattr(namespa ce,"name")
            >or locals()["name"]
            >or globals()["name"]
            >
            admin, I want to be sure you understand the advice you've been given.
            Yes, it is possible to "double dereference" through named variables;
            HOWEVER, it is essentially *never* advantageous to do so in application
            programming with Python (nor is it in Perl and PHP, despite what many
            senior people there teach). Use a dictionary, perhaps one with
            multi-dimensional keys.
            >
            Yes, I understand. I just reply to OP question not searching the reason why
            he wants to manage *variables* this way (he talk about dict, so I hope he
            know that they can be used to map names to values).
            So, this is not an advice, just technical ways related to the question as it
            was written.
            >
            And yes, personnally i use dictionnaries for such purpose.
            >
            A+
            >
            Laurent.- Hide quoted text -
            >
            - Show quoted text -

            Comment

            • 7stud

              #7
              Re: Store variable name in another variable

              On May 1, 6:43 am, loial <a...@loial.co. ukwrote:
              OK, I have it working with dictionaries.
              >
              However I have another scenerio :
              >
              I am reading a file containing records like the following :
              >
              <IMPOSITION_DOC UMENT>
              <WFBAN_PAGE>
              <WFBAN_START_DE TAILS name=\""MYVARIA BLE"\">
              ..
              ..
              >
              I need to substitute MYVARIABLE with the current value of MYVARIABLE
              in my python script and write the file out again.
              >
              The file may contain many more lines and many substitution values on
              any line
              >
              Assuming that MYVARIABLE is currently set to JOHN then the output
              would be
              >
              <IMPOSITION_DOC UMENT>
              <WFBAN_PAGE>
              <WFBAN_START_DE TAILS name="JOHN">
              >
              Can this be done in Python?

              s = "hello world, goodbye world"
              result = s.replace("worl d", "moon")
              print result

              Comment

              • Diez B. Roggisch

                #8
                Re: Store variable name in another variable

                loial schrieb:
                OK, I have it working with dictionaries.
                >
                However I have another scenerio :
                >
                I am reading a file containing records like the following :
                >
                <IMPOSITION_DOC UMENT>
                <WFBAN_PAGE>
                <WFBAN_START_DE TAILS name=\""MYVARIA BLE"\">
                ..
                ..
                >
                >
                I need to substitute MYVARIABLE with the current value of MYVARIABLE
                in my python script and write the file out again.
                >
                The file may contain many more lines and many substitution values on
                any line
                >
                Assuming that MYVARIABLE is currently set to JOHN then the output
                would be
                >
                <IMPOSITION_DOC UMENT>
                <WFBAN_PAGE>
                <WFBAN_START_DE TAILS name="JOHN">
                >
                Can this be done in Python? Amending the way the variable names are
                distinguished in the incoming file is possible if that would help.
                There are many ways to do so in python. You can use the built-in string
                interpolation:

                "<tag value="%(name)s "/>" % dict(name=value )

                Or you can use one of the many available templating engines, like KID,
                genshi, cheetah and so on.

                Diez

                Comment

                Working...