Python 3: exec arg 1

A

Alan G Isaac

In Python 3, you can no longer ``exec(open(filename))``.

I guess the reason is that in 3.0 ``open`` returns a stream,
instead of open file, and exec wants
"a string, bytes, or code object" and not a "TextIOWrapper".
So it returns an error.

Is it intentional that ``exec`` cannot handle a TextIOWrapper?

Bottom line: has ``execfile(filename)`` really become
``exec(open(filename).read())``? Is this a good thing?

Thanks,
Alan Isaac
 
T

Terry Reedy

Alan said:
In Python 3, you can no longer ``exec(open(filename))``.

I guess the reason is that in 3.0 ``open`` returns a stream,
instead of open file, and exec wants
"a string, bytes, or code object" and not a "TextIOWrapper".
So it returns an error.

Is it intentional that ``exec`` cannot handle a TextIOWrapper?

Bottom line: has ``execfile(filename)`` really become
``exec(open(filename).read())``? Is this a good thing?

Yes. Yes.
 
T

Terry Reedy

Alan said:

This: execfile(filename)
is a trivial (9 keystroke) abbreviation of
this: exec(open(filename).read())
which is similar in effect to
this: from filename import *
so it is really not needed.
 
A

Alan G Isaac

Alan said:
This: execfile(filename)
is a trivial (9 keystroke) abbreviation of
this: exec(open(filename).read())
which is similar in effect to
this: from filename import *
so it is really not needed.


Well, that does not really answer my question, imo.
I do not much care about the disappearance of ``execfile``.
I was asking, why is it a **good thing** that
``exec`` does not accept a TextIOWrapper?
Or is it just not implemented yet?
What is the gain from this particular backwards
incompatibility (in the sense that ``exec(open(fname))``
no longer works)?

Context: I used to be able to tell students they
could run their scripts from the interpreter
prompt with ``execfile(fname)``. I expected to
be able to tell them to ``exec( open(fname) )``,
which worked in Python 2. Now instead it is
``exec( open(filename).read() )`` which is a bit
more mysterious to a newbie. (Note: I teach
economics, not computer science.) And despite your
claim above, you know that ``import`` is not
really equivalent, and even more so now that
``reload`` must be imported.

Thanks,
Alan Isaac
 
S

Steven D'Aprano

Well, that does not really answer my question, imo. I do not much care
about the disappearance of ``execfile``. I was asking, why is it a
**good thing** that ``exec`` does not accept a TextIOWrapper?


I'm not sure if this is a stupid question or not, but what's a
TextIOWrapper? In the example you give:

exec(open(fname))

the argument to exec -- open(fname) -- is a file object:
<type 'file'>


BTW, exec is a statement. The brackets there are totally superfluous. You
can, and should, write:

exec open(fname)

Or is it just not implemented yet?
What is the gain from this particular backwards incompatibility (in the
sense that ``exec(open(fname))`` no longer works)?

Simplicity of implementation?

Context: I used to be able to tell students they could run their scripts
from the interpreter prompt with ``execfile(fname)``. I expected to be
able to tell them to ``exec( open(fname) )``, which worked in Python 2.
Now instead it is ``exec( open(filename).read() )`` which is a bit more
mysterious to a newbie. (Note: I teach economics, not computer
science.)

Why don't you give your students a site.py module containing

def execfile(fname):
exec open(fname).read()

and tell them to do "from site import execfile"?

If you control the environment they are running their programs on (school
computers?) then you can put a startup file in their path and have
execfile imported automatically.
 
A

Alan G Isaac

I do not much care about the disappearance of ``execfile``.
I was asking, why is it a **good thing** that
``exec`` does not accept a TextIOWrapper?
Or is it just not implemented yet?
What is the gain from this particular backwards
incompatibility (in the sense that ``exec(open(fname))``
no longer works)?

Still interested in an answer...
Alan Isaac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top