Velocity Reviews > How do I do this? (eval() on the left hand side)

# How do I do this? (eval() on the left hand side)

It's me
Guest
Posts: n/a

 12-07-2004
I am new to the Python language.

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

??

Thanks,

--
It's me

It's me
Guest
Posts: n/a

 12-07-2004

"Caleb Hattingh" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Hi It's me
>
> >
> > a = 3
> > y = "a"
> > print eval(y)
> >

>
> To get 'a' to be 4 here, you would say
>
> a = 4
>

Obviously but that's not what I wish to do.

> I am not sure why you would want to do otherwise? Perhaps you could
> sketch out a little more about what you are trying to do? That would help
> a lot. Are you aiming for something like pointer emulation with simple
> datatypes?
>

In REXX, for instance, one can do a:

interpret y' = 4'

Since y contains a, then the above statement amongs to:

a = 4

There are many situations where this is useful. For instance, you might be
getting an input which is a string representing the name of a variable and
you wish to evaluate the expression (like a calculator application, for
instance).

> Thanks
> Caleb

Russell Blau
Guest
Posts: n/a

 12-07-2004
"It's me" <(E-Mail Removed)> wrote in message
news:Y0ptd.39767\$(E-Mail Removed). com...
>
> In REXX, for instance, one can do a:
>
> interpret y' = 4'
>
> Since y contains a, then the above statement amongs to:
>
> a = 4
>
> There are many situations where this is useful. For instance, you might

be
> getting an input which is a string representing the name of a variable and
> you wish to evaluate the expression (like a calculator application, for
> instance).

In Python, the canonical advice for this situation is, "Use a dictionary."
separate from your application's namespace. Plus it's easier to debug and
maintain the code.

But, if you absolutely, positively have to refer to your variable
indirectly, you could do:

exec "%s = 4" % y

If y refers to the string "a", this will cause the variable a to refer to
the value 4.

--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.

Erik Max Francis
Guest
Posts: n/a

 12-07-2004
It's me wrote:

> In REXX, for instance, one can do a:
>
> interpret y' = 4'
>
> Since y contains a, then the above statement amongs to:
>
> a = 4

The direct equivalent in Python would be

a = 3
y = 'a'
exec '%s = 4' % y

The better question would be whether or not this as useful as one might
thing in Python; if you find yourself doing this, often there are better
ways to accomplish the same thing, such as using dictionaries.

--
Erik Max Francis && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
But the system has no wisdom / The Devil split us in pairs
-- Public Enemy

Craig Ringer
Guest
Posts: n/a

 12-07-2004
On Wed, 2004-12-08 at 05:12, It's me wrote:

> There are many situations where this is useful. For instance, you might be
> getting an input which is a string representing the name of a variable and
> you wish to evaluate the expression (like a calculator application, for
> instance).

While I do agree it can be handy, I also think that most possible uses
are also _very_ _dangerous_ security-wise. If possible it'd be safer to
write your code a different way to avoid the evaluation of user-supplied
expressions. For example, you could use a dictionary to store the
'user-accessible' namespace and have all their operations act on that.

You could probably do something _like_ what you want with exec() and
repr, but it'll break as soon as it encounters input that repr can't
make an exec()able string from. It's also really ugly.

If you know what namespace you want to modify ahead of time, or can pass
it to the function doing the modifying, you can also use
getattr()/setattr() or dict methods to do it. For example:

>>> # modify the globals space on the __main__ module
>>> import __main__
>>> varname = 'fred'
>>> setattr(__main__, varname, 'blech')
>>> fred

'blech'
>>> # same thing
>>> __main__.__dict__[varname] = 'yech!'
>>> fred

'yech!'
>>> # modify the attributes of some random object
>>> class dummy(object):

.... pass
....
>>> obj = dummy()
>>> setattr(obj, varname, 'eew')
>>> obj.fred

'eew'
>>> # same thing using the object's __dict__, NOT RECOMMENDED
>>> # outside the class's own methods.
>>> obj.__dict__[varname] = 'unwise'
>>> obj.fred

'unwise'

This, however, won't do you much good if you don't know what you'll be
modifying. I know the locals() and globals() functions exist, but have
always been leery of the idea of modifying their contents, and am not
sure what circumstances you could do so under even if you felt like
doing so.

In general, it'll be _much_ safer to use a generic object with
getattr/setattr or a dict than to try to work with your local or global
namespaces like this...

--
Craig Ringer

Steven Bethard
Guest
Posts: n/a

 12-07-2004
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

It's me
Guest
Posts: n/a

 12-07-2004
Yes, Russell, what you suggested works.

I have to chew more on the syntax to see how this is working.

because in the book that I have, it says:

exec code [ in globaldict [, localdict] ]

....

--
It's me

"Russell Blau" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "It's me" <(E-Mail Removed)> wrote in message
> news:Y0ptd.39767\$(E-Mail Removed). com...
> >
> > In REXX, for instance, one can do a:
> >
> > interpret y' = 4'
> >
> > Since y contains a, then the above statement amongs to:
> >
> > a = 4
> >
> > There are many situations where this is useful. For instance, you

might
> be
> > getting an input which is a string representing the name of a variable

and
> > you wish to evaluate the expression (like a calculator application, for
> > instance).

>
> In Python, the canonical advice for this situation is, "Use a dictionary."
> This has a number of advantages, including keeping your user's namespace
> separate from your application's namespace. Plus it's easier to debug and
> maintain the code.
>
> But, if you absolutely, positively have to refer to your variable
> indirectly, you could do:
>
> exec "%s = 4" % y
>
> If y refers to the string "a", this will cause the variable a to refer to
> the value 4.
>
> --
> I don't actually read my hotmail account, but you can replace hotmail with
> excite if you really want to reach me.
>
>

It's me
Guest
Posts: n/a

 12-07-2004
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" <(E-Mail Removed)> wrote in message
news:Ypptd.152996\$V41.76678@attbi_s52...
> 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

Steven Bethard
Guest
Posts: n/a

 12-07-2004
It's me wrote:
> 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>

If I had a situation like this, I'd probably store my 'variables' as
keys in a dict, e.g.:

>>> bindings = {}
>>> for i in range(3):

.... name = raw_input('Name: ')
.... value = int(raw_input('Value for %r: ' % name))
.... bindings[name] = value
....
<... after inputting 'eggs', '7', 'badger', '42', 'spam', '13' ...>
>>> bindings

{'eggs': 7, 'badger': 42, 'spam': 13}

Once you have the 'variables' in a dict, you can just use the dict
values in any expressions you need.

294

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

Well, it can do a lot, but the folks on this list are helpful enough to
mention when things you *can* do aren't necessarily things you *want* to
do.

Steve

Caleb Hattingh
Guest
Posts: n/a

 12-08-2004
Hi It's me

>
> a = 3
> y = "a"
> print eval(y)
>

To get 'a' to be 4 here, you would say

a = 4

I am not sure why you would want to do otherwise? Perhaps you could
sketch out a little more about what you are trying to do? That would help
a lot. Are you aiming for something like pointer emulation with simple
datatypes?

Thanks
Caleb