"Variable variable name" or "variable lvalue"

Discussion in 'Python' started by mfglinux, Aug 15, 2007.

  1. mfglinux

    mfglinux Guest

    Hello to everybody

    I would like to know how to declare in python a "variable name" that
    it is in turn a variable
    In bash shell I would wrote sthg like:

    for x in `seq 1 3`
    do
    M$i=Material(x) #Material is a python class
    done

    Why I need this? Cause I have a python module that obliges me to build
    a variable called Period, which should have a variable name of
    summands (depends on the value of x)

    #Let's say x=3, then Period definition is
    Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
    python class

    I dont know how to automatize last piece of code for any x

    thank you

    Marcos
     
    mfglinux, Aug 15, 2007
    #1
    1. Advertising

  2. On Wed, 15 Aug 2007 10:42:02 -0700, mfglinux wrote:

    > I would like to know how to declare in python a "variable name" that
    > it is in turn a variable
    > In bash shell I would wrote sthg like:
    >
    > for x in `seq 1 3`
    > do
    > M$i=Material(x) #Material is a python class
    > done


    You want a dictionary.

    M = dict()
    for x in xrange(1, 4):
    M[x] = Material(x)

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Aug 15, 2007
    #2
    1. Advertising

  3. mfglinux

    Larry Bates Guest

    mfglinux wrote:
    > Hello to everybody
    >
    > I would like to know how to declare in python a "variable name" that
    > it is in turn a variable
    > In bash shell I would wrote sthg like:
    >
    > for x in `seq 1 3`
    > do
    > M$i=Material(x) #Material is a python class
    > done
    >
    > Why I need this? Cause I have a python module that obliges me to build
    > a variable called Period, which should have a variable name of
    > summands (depends on the value of x)
    >
    > #Let's say x=3, then Period definition is
    > Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
    > python class
    >
    > I dont know how to automatize last piece of code for any x
    >
    > thank you
    >
    > Marcos
    >

    I think the answer is to use a dictionary of lists , but it is a little hard to
    tell from your description:

    mdict={1:[Material1, 12.5],
    2:[Material2, 25.0],
    3:[Material3, 12.5]
    }

    x=3
    slab_arg=0
    for i in range(1,x):
    func, farg=mdict
    slab_arg+=func(farg)

    Period=Slab(slab_arg)

    Obviously not tested!

    I sense that the Material functions should be consolidated into something more
    general here.

    -Larry Bates
     
    Larry Bates, Aug 15, 2007
    #3
  4. On 8/15/07, mfglinux <> wrote:
    > Hello to everybody
    >
    > I would like to know how to declare in python a "variable name" that
    > it is in turn a variable
    > In bash shell I would wrote sthg like:
    >
    > for x in `seq 1 3`
    > do
    > M$i=Material(x) #Material is a python class
    > done
    >
    > Why I need this? Cause I have a python module that obliges me to build
    > a variable called Period, which should have a variable name of
    > summands (depends on the value of x)
    >
    > #Let's say x=3, then Period definition is
    > Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
    > python class
    >
    > I dont know how to automatize last piece of code for any x
    >
    > thank you
    >
    > Marcos
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    You could use a dictionary -- just build the dictionary keys using
    your loop and assign values.
     
    Shawn Milochik, Aug 15, 2007
    #4
  5. mfglinux

    Peter Otten Guest

    mfglinux wrote:

    > Hello to everybody
    >
    > I would like to know how to declare in python a "variable name" that
    > it is in turn a variable
    > In bash shell I would wrote sthg like:
    >
    > for x in `seq 1 3`
    > do
    > M$i=Material(x) #Material is a python class
    > done


    In Python you would build a list instead of inventing variable names:

    numbers = [12.5, 25, 12.5]
    materials = []
    for x in numbers:
    materials.append(Material(x))


    > Why I need this? Cause I have a python module that obliges me to build
    > a variable called Period, which should have a variable name of
    > summands (depends on the value of x)
    >
    > #Let's say x=3, then Period definition is
    > Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
    > python class
    >
    > I dont know how to automatize last piece of code for any x


    You can use another loop to to "sum" over the materials and then feed the
    result to the Slab constructor:

    accu = materials[0]
    for material in materials[1:]:
    accu += material
    period = Slab(accu)

    If you want to simplify things somewhat you can merge the two loops into
    one:

    numbers = [12.5, 25, 12.5]
    accu = Material(numbers[0])
    for x in numbers[1:]:
    accu += Material(x)
    period = Slab(accu)

    Or you try your hands on a bit of functional programming:

    from operator import add
    numbers = [12.5, 25, 12.5]
    period = Slab(reduce(add, (Material(x) for x in numbers)))

    Peter
     
    Peter Otten, Aug 15, 2007
    #5
  6. On Wed, 15 Aug 2007 10:42:02 -0700, mfglinux wrote:

    > Hello to everybody
    >
    > I would like to know how to declare in python a "variable name" that
    > it is in turn a variable
    > In bash shell I would wrote sthg like:
    >
    > for x in `seq 1 3`
    > do
    > M$i=Material(x) #Material is a python class
    > done
    >
    > Why I need this? Cause I have a python module that obliges me to build
    > a variable called Period, which should have a variable name of
    > summands (depends on the value of x)
    >
    > #Let's say x=3, then Period definition is
    > Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
    > python class
    >
    > I dont know how to automatize last piece of code for any x
    >
    > thank you
    >
    > Marcos


    It sounds to me like you want python's "eval", based on my
    understanding of how that bash code should work. It's not that different
    from eval in bash, though in python you'd need it; in bash you don't in
    this case.

    So you could probably do something like (untested):

    for x in xrange(1,4):
    eval 'M%d=Material(x)' % x
     
    Dan Stromberg - Datallegro, Aug 16, 2007
    #6
  7. mfglinux

    mfglinux Guest

    The solution with the dictionary worked perfectlly well, my script is
    running and even produces data with sense!!!

    Thank you very much indeed to all of you answering. Cheers!
     
    mfglinux, Aug 16, 2007
    #7
  8. mfglinux

    Guest

    On Aug 15, 1:42 pm, mfglinux <> wrote:
    > Hello to everybody
    >
    > I would like to know how to declare in python a "variable name" that
    > it is in turn a variable
    > In bash shell I would wrote sthg like:
    >
    > for x in `seq 1 3`
    > do
    > M$i=Material(x) #Material is a python class
    > done
    >
    > Why I need this? Cause I have a python module that obliges me to build
    > a variable called Period, which should have a variable name of
    > summands (depends on the value of x)
    >
    > #Let's say x=3, then Period definition is
    > Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
    > python class
    >
    > I dont know how to automatize last piece of code for any x
    >
    > thank you
    >
    > Marcos


    Regardless of whether or not this is a "best practice" sometimes it is
    necessary. For example, I am looping through a dictionary to set some
    class properties. Anyway, here is what I finally came up with:

    exec "self.%s = '%s'" % (item, plist[item])

    A more simple example for setting a variable outside of a class...

    exec '%s = '%s'" % ('variableName', 'variable value')

    Cheers!
    Mike
     
    , Aug 19, 2007
    #8
  9. mfglinux

    Gary Herron Guest

    wrote:
    > On Aug 15, 1:42 pm, mfglinux <> wrote:
    >
    >> Hello to everybody
    >>
    >> I would like to know how to declare in python a "variable name" that
    >> it is in turn a variable
    >> In bash shell I would wrote sthg like:
    >>
    >> for x in `seq 1 3`
    >> do
    >> M$i=Material(x) #Material is a python class
    >> done
    >>
    >> Why I need this? Cause I have a python module that obliges me to build
    >> a variable called Period, which should have a variable name of
    >> summands (depends on the value of x)
    >>
    >> #Let's say x=3, then Period definition is
    >> Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
    >> python class
    >>
    >> I dont know how to automatize last piece of code for any x
    >>
    >> thank you
    >>
    >> Marcos
    >>

    >
    > Regardless of whether or not this is a "best practice" sometimes it is
    > necessary. For example, I am looping through a dictionary to set some
    > class properties. Anyway, here is what I finally came up with:
    >
    > exec "self.%s = '%s'" % (item, plist[item])
    >

    Yuck! Not at all necessary. Use setattr instead:

    setattr(self, item, plist[item])

    That's much cleaner then an exec or eval. You may also find getattr and
    hasattr useful.

    Gary Herron



    > A more simple example for setting a variable outside of a class...
    >
    > exec '%s = '%s'" % ('variableName', 'variable value')
    >
    > Cheers!
    > Mike
    >
    >
     
    Gary Herron, Aug 19, 2007
    #9
  10. mfglinux

    Steve Holden Guest

    Gary Herron wrote:
    > wrote:
    >> On Aug 15, 1:42 pm, mfglinux <> wrote:
    >>
    >>> Hello to everybody
    >>>
    >>> I would like to know how to declare in python a "variable name" that
    >>> it is in turn a variable
    >>> In bash shell I would wrote sthg like:
    >>>
    >>> for x in `seq 1 3`
    >>> do
    >>> M$i=Material(x) #Material is a python class
    >>> done
    >>>
    >>> Why I need this? Cause I have a python module that obliges me to build
    >>> a variable called Period, which should have a variable name of
    >>> summands (depends on the value of x)
    >>>
    >>> #Let's say x=3, then Period definition is
    >>> Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
    >>> python class
    >>>
    >>> I dont know how to automatize last piece of code for any x
    >>>
    >>> thank you
    >>>
    >>> Marcos
    >>>

    >> Regardless of whether or not this is a "best practice" sometimes it is
    >> necessary. For example, I am looping through a dictionary to set some
    >> class properties. Anyway, here is what I finally came up with:
    >>
    >> exec "self.%s = '%s'" % (item, plist[item])
    >>

    > Yuck! Not at all necessary. Use setattr instead:
    >
    > setattr(self, item, plist[item])
    >
    > That's much cleaner then an exec or eval. You may also find getattr and
    > hasattr useful.
    >

    Or even, in some cases,

    self.__dict__.update(otherdict)

    if you have a dictionary of stuff to put into an object.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
     
    Steve Holden, Aug 20, 2007
    #10
  11. mfglinux

    Guest

    On Aug 19, 1:10 pm, Gary Herron <> wrote:
    > wrote:
    > > On Aug 15, 1:42 pm, mfglinux <> wrote:

    >
    > >> Hello to everybody

    >
    > >> I would like to know how to declare in python a "variable name" that
    > >> it is in turn a variable
    > >> In bash shell I would wrote sthg like:

    >
    > >> for x in `seq 1 3`
    > >> do
    > >> M$i=Material(x) #Material is a python class
    > >> done

    >
    > >> Why I need this? Cause I have a python module that obliges me to build
    > >> a variable called Period, which should have a variable name of
    > >> summands (depends on the value of x)

    >
    > >> #Let's say x=3, then Period definition is
    > >> Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
    > >> python class

    >
    > >> I dont know how to automatize last piece of code for any x

    >
    > >> thank you

    >
    > >> Marcos

    >
    > > Regardless of whether or not this is a "best practice" sometimes it is
    > > necessary. For example, I am looping through a dictionary to set some
    > > class properties. Anyway, here is what I finally came up with:

    >
    > > exec "self.%s = '%s'" % (item, plist[item])

    >
    > Yuck! Not at all necessary. Use setattr instead:
    >
    > setattr(self, item, plist[item])
    >
    > That's much cleaner then an exec or eval. You may also find getattr and
    > hasattr useful.
    >
    > Gary Herron
    >
    > > A more simple example for setting a variable outside of a class...

    >
    > > exec '%s = '%s'" % ('variableName', 'variable value')

    >
    > > Cheers!
    > > Mike


    Thanks! I'm still getting used to Python's nifty features.
     
    , Sep 12, 2007
    #11
  12. On Aug 15, 4:19 pm, Peter Otten <> wrote:
    > If you want to simplify things somewhat you can merge the two loops into
    > one:
    >
    > numbers = [12.5, 25, 12.5]
    > accu = Material(numbers[0])
    > for x in numbers[1:]:
    > accu += Material(x)
    > period = Slab(accu)


    Better to use the `sum' builtin and a generator expression:

    period = Slab(sum(Material(x)) for x in numbers)

    --
    Roberto Bonvallet
     
    Roberto Bonvallet, Sep 12, 2007
    #12
    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. Alex Johnson

    how to construct a variable, variable name ?

    Alex Johnson, Aug 1, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    83
    J├╝rgen Exner
    Aug 1, 2003
Loading...

Share This Page