473,396 Members | 1,853 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 with print stmt

Art
Hello,

I can't seem to get eval or compile to accept the print stmt, or any
complicated statements.

The documentation I've looked out doesn't explain why this is.

ex:
eval("print('foo')") Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
print('foo')

but this works: eval('sys.__stdout__.write("foo\\n")') foo

also, I can't get eval to make callable objects. is this possible? eval("def foo():\n return 'foo'")

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
def foo():
^
SyntaxError: invalid syntax

I'd like to have a command line parameter be turned into a callable
statement with an expected environment (a la awk) but I can't seem to
get this working. (I get the same behavior for compile.

Thanks,
A
Jul 18 '05 #1
1 1734
In article <6c**************************@posting.google.com >,
ab****@prontomail.com (Art) wrote:
Hello,

I can't seem to get eval or compile to accept the print stmt, or any
complicated statements.

The documentation I've looked out doesn't explain why this is.


eval only works with expressions, and `print ...` is a statement.
So is `def ...` .

exec should do what you want.

Regards. Mel.
Jul 18 '05 #2

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...
6
by: Bob Rogers | last post by:
Given this class: class C(object): def set_x(self, x): self._x = x def get_x(self): return self._x x = property(get_x, set_x)
2
by: pasa | last post by:
I'm an old time python user, but just got bitten by namespaces in eval. If this is an old discussion somewhere, feel free to point me there. Based on the documentation, I would have expected the...
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: rayw | last post by:
Could someone give me the name of a compiler/environment where this code evaluates left to right (or, at least not right to left!) - as in the compiler evaluates the parameter list left to right....
24
by: Larry | last post by:
Hi there: I have seen numerous postings about eval() and its evils on this forum. However, one of our developers is using it in the following way, which seems like a great use of it. Page...
1
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php...
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...
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.