473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

eval() in python

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
5 2315
> 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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Leif K-Brooks | last post by:
In Python 2.4, although None can't be directly assigned to, globals() can still be; however, that won't change the value of the expression "None" in ordinary statements. Except with the eval...
3
by: Jeremy Sanders | last post by:
Hi - I'm trying to subclass a dict which is used as the globals environment of an eval expression. For instance: class Foo(dict): def __init__(self): self.update(globals()) self = 42
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
8
by: abhishek | last post by:
>>a,b=3,4 7 Now I want to evaluate y by substituting for the evaluated value of x. eval(y) will try to add "a+b" to 3 and return an error. I could do this, 10 but this becomes unwieldy if I...
4
by: Jm lists | last post by:
Hello members, I want to know does the "eval" in python have the same features as in Perl (capture errors)? For example,in perl I can wrote: $re = eval { 1 / 0 }; Though 1/0 is a fatal...
6
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
1
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner:...
0
by: J. Clifford Dyer | last post by:
On Tue, 2007-12-11 at 16:55 -0800, katie smith wrote: Katie, First, please provide a useful subject heading when posting to the list. It makes everyone's life easier when searching the...
0
by: Lie Ryan | last post by:
On Tue, 30 Sep 2008 16:04:34 -0500, William Purcell wrote: when you pass mydict, it is used as the global variables in the eval, right? Then, you passed a code to eval('...', mydict), sometimes...
3
by: James Mills | last post by:
On Thu, Oct 9, 2008 at 2:26 PM, Warren DeLano <warren@delsci.comwrote: Yes it does :) I second this. It's far better to use Data Structures rather than Programming Constructs
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.