473,395 Members | 1,652 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,395 software developers and data experts.

eval or execute, is this the (most) correct way ?

hello,

I'm trying to make an editor with an integrated Shell.
>>So when type:
2+5
>>I want the answer on the next line:
7
>>When I type:
myvar = 55
myvar
>>I want the value of myvar:
55

So AFAIK, sometimes I've to use eval and sometimes I need exec,
so I use the following code (global / local dictionary parameters are
left out in this example):

try:
print eval ( line )
except :
exec ( line )

Is this the (most) correct / elegant way, or are there better solutions ?

I read somewhere that exec is going to disappear,
and noticed that in 2.5 (I just updated to) the doc is already disapeared,
is this no longer possible in Python 3 ?

thanks,
Stef Mientki
Aug 11 '08 #1
6 1128
On Mon, 11 Aug 2008 17:26:56 +0200, Stef Mientki wrote:
I'm trying to make an editor with an integrated Shell.
....
Is this the (most) correct / elegant way, or are there better solutions
?

The best solution is not to re-invent the wheel: "import code" is the way
to emulate Python's interactive interpreter. Try running "python -m code"
at a regular shell (not the Python shell, your operating system's shell).

Doing a search of the file code.py, I don't find the string "eval" at
all. My guess is that your approach is probably not the best way.

--
Steven
Aug 11 '08 #2
Steven D'Aprano wrote:
On Mon, 11 Aug 2008 17:26:56 +0200, Stef Mientki wrote:

>I'm trying to make an editor with an integrated Shell.
...
>Is this the (most) correct / elegant way, or are there better solutions
?


The best solution is not to re-invent the wheel: "import code" is the way
to emulate Python's interactive interpreter.
sorry, but that confuses me even more,
I don;t have a file / module,
just a workspace and one or more lines of code in memory.
Try running "python -m code"
at a regular shell (not the Python shell, your operating system's shell).
I might have been not clear enough,
I'm trying to build an python-IDE,
so I definitely want to run code from memory.

cheers,
Stef
Doing a search of the file code.py, I don't find the string "eval" at
all. My guess is that your approach is probably not the best way.

Aug 11 '08 #3
So AFAIK, sometimes I've to use eval and sometimes I need exec,
so I use the following code (global / local dictionary parameters are
left out in this example):
Is this the (most) correct / elegant way, or are there better solutions ?
You should be using compile with the "single" start symbol, and then
use eval on the resulting code option.
I read somewhere that exec is going to disappear,
That's not true. exec stops being a statement, and becomes a function
(like print).

Regards,
Martin
Aug 11 '08 #4
Martin v. Löwis wrote:
>So AFAIK, sometimes I've to use eval and sometimes I need exec,
so I use the following code (global / local dictionary parameters are
left out in this example):
Is this the (most) correct / elegant way, or are there better solutions ?

You should be using compile with the "single" start symbol, and then
use eval on the resulting code option.

thanks Martin,
but when I read the doc (of one of the many) "compile" functions,
I see 2 problems:
- I still have to provide "kind" as exec or eval
- I can not specify the global and local namespace (which is essential
for me)
>I read somewhere that exec is going to disappear,

That's not true. exec stops being a statement, and becomes a function
(like print).

That's good to hear,
as I already didn't realize it could also be used as a statement ;-)

cheers,
Stef
Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list
Aug 11 '08 #5


Stef Mientki wrote:
Steven D'Aprano wrote:
>The best solution is not to re-invent the wheel: "import code" is the
way to emulate Python's interactive interpreter.
sorry, but that confuses me even more,
"The code module provides facilities to implement read-eval-print loops
in Python. Two classes and convenience functions are included which can
be used to build applications which provide an interactive interpreter
prompt." IDLE uses it to do just that. File idelib/pyshell.py imports
InteractiveInterpreter. A quarter of the file is the definition of
class ModifiedInterpreter(InteractiveInterpreter):
I don;t have a file / module,
just a workspace and one or more lines of code in memory.
The code module has many options. "InteractiveConsole.push(line)
Push a line of source text to the interpreter. "

Anyway, your first post indicated that those 'lines of code in memory'
originate from console input, which, I believe, is the default input
source for the classes. You will need to experiment to see just how
they work.
>Doing a search of the file code.py, I don't find the string "eval" at
all. My guess is that your approach is probably not the best way.
It would use 'exec' on statements, even if they happen to be expression
statements. Exec is not going away. It is a built-in function in 3.0.

Terry Jan Reedy

Aug 11 '08 #6
>You should be using compile with the "single" start symbol, and then
>use eval on the resulting code option.

thanks Martin,
but when I read the doc (of one of the many) "compile" functions,
I see 2 problems:
- I still have to provide "kind" as exec or eval
No, you can also use "single".
- I can not specify the global and local namespace (which is essential
for me)
You do so not in compile, but in eval.
That's good to hear,
as I already didn't realize it could also be used as a statement ;-)
It *is* a statement, and always was. A "statement" is a fragment of
code that just gets executed, and doesn't produce a value (unlike
an expression, which does produce a value). So you can't write

x = exec "1+1"

or

foo(exec)

whereas you *can* write

x = eval("1+1")

and

foo(eval)

Likewise, you cannot write in Python 2.x, but can write in 3.x

x = print("Hello")

and

foo(print)

Things like "for", "while", "return", and assignments are statements,
things like "+", "**", lambda, function calls are expressions. print
and exec are statements in 2.x, and functions (thus, expressions)
in 3.x.

Regards,
Martin
Aug 11 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mike | last post by:
After reading much on the evils of eval, I have a question using my own personal use of the function... We have a reports system that will generate reports based on a number of parameters...
11
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
6
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.