Declaring variables from a list

Discussion in 'Python' started by Cactus, Apr 8, 2005.

  1. Cactus

    Cactus Guest

    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
    Cactus, Apr 8, 2005
    #1
    1. Advertising

  2. "Cactus" wrote:

    > 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?


    why?

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

    </F>
    Fredrik Lundh, Apr 8, 2005
    #2
    1. Advertising

  3. Python has a builtin function called locals which returns the local
    context as a dictionary

    >>> locals = locals()
    >>> locals["a"] = 5
    >>> a

    5
    >>> locals["a"] = "changed"
    >>> a

    'changed'

    On 8 Apr 2005 13:55:39 -0700, Cactus <> wrote:
    > 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
    >



    --
    http://blogs.applibase.net/sidharth
    Sidharth Kuruvila, Apr 8, 2005
    #3
  4. Cactus

    Inyeol Lee Guest

    On Sat, Apr 09, 2005 at 03:15:01AM +0530, Sidharth Kuruvila wrote:
    > Python has a builtin function called locals which returns the local
    > context as a dictionary
    >
    > >>> locals = locals()
    > >>> locals["a"] = 5
    > >>> a

    > 5
    > >>> locals["a"] = "changed"
    > >>> a

    > 'changed'


    >From Python lib reference:


    """
    locals()
    ...
    Warning: The contents of this dictionary should not be
    modified; changes may not affect the values of local variables used
    by the interpreter.
    """
    Inyeol Lee, Apr 8, 2005
    #4
  5. 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 <> wrote:
    > On Sat, Apr 09, 2005 at 03:15:01AM +0530, Sidharth Kuruvila wrote:
    > > Python has a builtin function called locals which returns the local
    > > context as a dictionary
    > >
    > > >>> locals = locals()
    > > >>> locals["a"] = 5
    > > >>> a

    > > 5
    > > >>> locals["a"] = "changed"
    > > >>> a

    > > 'changed'

    >
    > >From Python lib reference:

    >
    > """
    > 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
    >



    --
    http://blogs.applibase.net/sidharth
    Sidharth Kuruvila, Apr 8, 2005
    #5
  6. 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
    Pierre Quentel, Apr 9, 2005
    #6
  7. Cactus

    Cactus Guest

    "Fredrik Lundh" <> wrote in message news:<>...
    > "Cactus" wrote:
    >
    > > 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?

    >
    > why?
    >
    > if you want a dictionary, use a dictionary (see the tutorial for details).
    >
    > </F>


    Thanks,

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

    Cacti
    Cactus, Apr 9, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mortal Wombat
    Replies:
    3
    Views:
    6,813
    Mortal Wombat
    Aug 7, 2003
  2. Rob Meade

    Declaring variables - best practice?

    Rob Meade, Nov 24, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    454
    Rob Meade
    Nov 24, 2003
  3. Steven K

    Newbie: Declaring Variables

    Steven K, Feb 27, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    421
    Alvin Bruney [MVP]
    Feb 28, 2004
  4. =?Utf-8?B?QXNoYQ==?=

    help in declaring variables

    =?Utf-8?B?QXNoYQ==?=, Dec 31, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    315
    John Saunders
    Dec 31, 2004
  5. Joey P
    Replies:
    0
    Views:
    549
    Joey P
    Oct 10, 2004
Loading...

Share This Page