[Tutor] eval use (directly by interpreter vs with in a script)

Danny Yoo dyoo at hashcollision.org
Mon Nov 3 06:37:53 CET 2014

> I use exec to jump to another program within the
> same directory, such as:
> execfile("BloodPressure02Sorting.py")
> and let the program terminate there. Should I do
> it differently or are you talking about a different
> horse?

This is related.

Rather than use execfile, you may want to consider looking into "modules".


By using the module system, you can call the functions of other files.
We might think of one of the functions in a file as the main program,
in which case you should be able to arrange a module approach that
effectively does what you're doing with execfile.

Using the module system is often nicer because the functions of
another module are still just regular functions: it's easy to pass
along Python values as arguments to tell the next process some
contextual information.  We can contrast this with an execfile
approach, where there's not such a nice way of passing on that context
to the other program.

The module approach also can "fail faster", in the case that when we
want to use a module, we "import" it at the head of our main program.
So if we got the file name wrong, we get fairly immediate feedback
about our mistake.  In contrast, the execfile approach defers from
touching the secondary program until we're at the point of the
execfile call itself.  So if we got the filename wrong, we don't see
the error as quickly.

More information about the Tutor mailing list