Python 3: exec arg 1

Alan G Isaac alan.isaac at
Sun Jan 18 15:36:15 CET 2009

>>> Alan G Isaac wrote:
>>>> 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?

>> On 1/17/2009 4:20 PM Terry Reedy apparently wrote:
>>> Yes. Yes.

> Alan G Isaac wrote:
>> OK.  Why?

On 1/17/2009 10:08 PM Terry Reedy apparently wrote:
> 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.

Alan Isaac

More information about the Python-list mailing list