Thanks for all the replies and yes I realize the associated issue of doing
something like this.

For simplicity sake, let's say I need to do something like this (for
whatever reason):

<prompt for name of variable in someother program space you wish to
retrieve>
<go retrieve the value from that other program>
<assign the retrieved value to a variable of the same name in Python>

In situations like this, I wouldn't know the name of the variable in Python
I need to use ahead of time and so I would have to somehow convert a string
to be used as variable.  Of course, I can create a dictionary to keep track
of which variable has what name and this method of using exec should be
avoid if at all possible.

I am just trying to understand the language and see what it can do.

--
It's me




"Steven Bethard" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> It's me wrote:
> > How do I do something like this:
> >
> > I know that
> >
> >     a = 3
> >     y = "a"
> >     print eval(y)
> >
> > would give me a print out of 3 - but how do I do something to the effect
of:
> >
> >     eval(y) = 4    # hopefully the value of a gets changed to 4
>
> Generally, if you find yourself doing this, you may want to rethink your
> program organization.  That being said, if you are in the global scope,
> one option looks something like:
>
>  >>> a = 3
>  >>> y = 'a'
>  >>> globals()[y] = 4
>  >>> a
> 4
>
> If you can give us some more context on why you want to do this, we can
> probably suggest a better approach.  There aren't too many places where
> even advanced Python programmers need to use eval...
>
> Steve


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to