By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,813 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,813 IT Pros & Developers. It's quick & easy.

eval() in python

P: n/a
is it possible to eval a string like the following?

m='''
i0=[0,1]
i1=[2,3]
i2=[4,'a']

h0=[]
for j0 in i0:
h1=[]
for j1 in i1:
h2=[]
for j2 in i2:
h2.append(f(j0,j1,j2))
h1.append( h2[:] )
h0.append( h1[:] )
return h0'''

perhaps i'm tired, but why can't i run:

t='m=3'
print eval(t)

the doc seems to suggest that eval is only for expressions... it says
uses exec for statements, but i don't seem to see a exec function?

Xah
xa*@xahlee.org
http://xahlee.org/

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> the doc seems to suggest that eval is only for expressions... it says
uses exec for statements, but i don't seem to see a exec function?


Python 2.4 (#1, Dec 30 2004, 08:00:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
s="print 'hello Xah Lee :-)'"
exec(s) hello Xah Lee :-)


- harold -

--
I wish there was a knob on the TV to turn up the intelligence. There's
a
knob called "brightness", but it doesn't seem to work.
-- Gallagher

Jul 19 '05 #2

P: n/a
Xah Lee wrote:
the doc seems to suggest that eval is only for expressions... it says
uses exec for statements, but i don't seem to see a exec function?


Because it's a statement: http://docs.python.org/ref/exec.html#l2h-563
Jul 19 '05 #3

P: n/a
On Tue, Jun 21, 2005 at 08:13:47AM -0400, Peter Hansen wrote:
Xah Lee wrote:
the doc seems to suggest that eval is only for expressions... it says
uses exec for statements, but i don't seem to see a exec function?


Because it's a statement: http://docs.python.org/ref/exec.html#l2h-563


but the documentation is sooooooo baaaaaaad that it makes babies cry and
maybe spreads herpes too.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCuAlUJd01MZaTXX0RAqtVAJsE6RPF5wIs5+g4OqxH4a r76ZSFDgCeO6bd
13SXkT2AdxaPxk9Ea/D1wj0=
=8V9f
-----END PGP SIGNATURE-----

Jul 19 '05 #4

P: n/a
harold fellermann wrote:
>>> s="print 'hello Xah Lee :-)'"
>>> exec(s)

hello Xah Lee :-)


Note that because "exec" is a statement, the parentheses above are
superfluous.
--
Benji York
Jul 19 '05 #5

P: n/a
"Xah Lee" schrieb

perhaps i'm tired, but why can't i run:

t='m=3'
print eval(t)

Perhaps you didn't read the documentation? :-)
Perhaps you didn't try hard enough?
C:\WINNT>c:\programme\python\python
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more
information.
t='m=3'
m Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'm' is not defined exec(t)
m 3


HTH
Martin
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.