Declaring variables from a list

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

    #1

    Declaring variables from a list

    Hi,

    If I got a list is it possible to declare a variable from the items in that list?

    Code Sample:
    Blob = ['Var1', 'Var2', 'vAR3']
    i = 5
    for listitems in Blob:
    i += 1
    listitems = i

    print Var1
    6
    print Var2
    7
    print vAR3
    8



    Something like that? This doesn't work (obviously) but is there a way to do this?

    TIA,
    Cacti
  • Fredrik Lundh

    #2
    Re: Declaring variables from a list

    "Cactus" wrote:
    [color=blue]
    > If I got a list is it possible to declare a variable from the items in that list?
    >
    > Code Sample:
    > Blob = ['Var1', 'Var2', 'vAR3']
    > i = 5
    > for listitems in Blob:
    > i += 1
    > listitems = i
    >
    > print Var1
    > 6
    > print Var2
    > 7
    > print vAR3
    > 8
    >[/color]
    [color=blue]
    > Something like that? This doesn't work (obviously) but is there a way to do this?[/color]

    why?

    if you want a dictionary, use a dictionary (see the tutorial for details).

    </F>



    Comment

    • Sidharth Kuruvila

      #3
      Re: Declaring variables from a list

      Python has a builtin function called locals which returns the local
      context as a dictionary
      [color=blue][color=green][color=darkred]
      >>> locals = locals()
      >>> locals["a"] = 5
      >>> a[/color][/color][/color]
      5[color=blue][color=green][color=darkred]
      >>> locals["a"] = "changed"
      >>> a[/color][/color][/color]
      'changed'

      On 8 Apr 2005 13:55:39 -0700, Cactus <le_cactus@msn. com> wrote:[color=blue]
      > Hi,
      >
      > If I got a list is it possible to declare a variable from the items in that list?
      >
      > Code Sample:
      > Blob = ['Var1', 'Var2', 'vAR3']
      > i = 5
      > for listitems in Blob:
      > i += 1
      > listitems = i
      >
      > print Var1
      > 6
      > print Var2
      > 7
      > print vAR3
      > 8
      >
      > Something like that? This doesn't work (obviously) but is there a way to do this?
      >
      > TIA,
      > Cacti
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]


      --

      Comment

      • Inyeol Lee

        #4
        Re: Declaring variables from a list

        On Sat, Apr 09, 2005 at 03:15:01AM +0530, Sidharth Kuruvila wrote:[color=blue]
        > Python has a builtin function called locals which returns the local
        > context as a dictionary
        >[color=green][color=darkred]
        > >>> locals = locals()
        > >>> locals["a"] = 5
        > >>> a[/color][/color]
        > 5[color=green][color=darkred]
        > >>> locals["a"] = "changed"
        > >>> a[/color][/color]
        > 'changed'[/color]
        [color=blue]
        >From Python lib reference:[/color]

        """
        locals()
        ...
        Warning: The contents of this dictionary should not be
        modified; changes may not affect the values of local variables used
        by the interpreter.
        """

        Comment

        • Sidharth Kuruvila

          #5
          Re: Declaring variables from a list

          What I gave was a bad solution. Something that works right now, but
          probably shouldn't be done.

          On Apr 9, 2005 3:37 AM, Inyeol Lee <inyeol.lee@sii mage.com> wrote:[color=blue]
          > On Sat, Apr 09, 2005 at 03:15:01AM +0530, Sidharth Kuruvila wrote:[color=green]
          > > Python has a builtin function called locals which returns the local
          > > context as a dictionary
          > >[color=darkred]
          > > >>> locals = locals()
          > > >>> locals["a"] = 5
          > > >>> a[/color]
          > > 5[color=darkred]
          > > >>> locals["a"] = "changed"
          > > >>> a[/color]
          > > 'changed'[/color]
          >[color=green]
          > >From Python lib reference:[/color]
          >
          > """
          > locals()
          > ...
          > Warning: The contents of this dictionary should not be
          > modified; changes may not affect the values of local variables used
          > by the interpreter.
          > """
          > --
          > http://mail.python.org/mailman/listinfo/python-list
          >[/color]


          --

          Comment

          • Pierre Quentel

            #6
            Re: Declaring variables from a list

            You can use the built-in statement exec
            (http://www.python.org/doc/2.4.1/ref/exec.html) :

            # Blob = ['Var1', 'Var2', 'vAR3']
            # i = 5
            # for listitems in Blob:
            # i += 1
            # exec('%s = i' %listitems)
            #
            # print Var1, Var2, vAR3

            Regards,
            Pierre

            Comment

            • Cactus

              #7
              Re: Declaring variables from a list

              "Fredrik Lundh" <fredrik@python ware.com> wrote in message news:<mailman.1 576.1112994277. 1799.python-list@python.org >...[color=blue]
              > "Cactus" wrote:
              >[color=green]
              > > If I got a list is it possible to declare a variable from the items in that list?
              > >
              > > Code Sample:
              > > Blob = ['Var1', 'Var2', 'vAR3']
              > > i = 5
              > > for listitems in Blob:
              > > i += 1
              > > listitems = i
              > >
              > > print Var1
              > > 6
              > > print Var2
              > > 7
              > > print vAR3
              > > 8
              > >[/color]
              >[color=green]
              > > Something like that? This doesn't work (obviously) but is there a way to do this?[/color]
              >
              > why?
              >
              > if you want a dictionary, use a dictionary (see the tutorial for details).
              >
              > </F>[/color]

              Thanks,

              I'll look in to that. Seems like that will work....

              Cacti

              Comment

              Working...