Ask Your Question

Revision history [back]

"I thought that the following are equivalent: entering a bunch of lines into the sage interpreter, hitting return after each one, or putting those same lines into a .sage file and then attaching that .sage file in the interpreter." They are not equivalent. When you load a .sage file, Sage reads the file into memory, runs the preparser on it, then calls the execfile command on the resulting string. It thus runs the entire contents of the file in one single call to execfile.

More precisely, here's the actual source code in the sage library, from src/sage/misc/preparser.py (https://github.com/sagemath/sage/blob/master/src/sage/misc/preparser.py):

elif fpath.endswith('.sage'):
    from sage.misc.attached_files import load_attach_mode
    load_debug_mode, attach_debug_mode = load_attach_mode()
    if (attach and attach_debug_mode) or ((not attach) and load_debug_mode):
        # Preparse to a file to enable tracebacks with
        # code snippets. Use preparse_file_named to make
        # the file name appear in the traceback as well.
        # See Trac 11812.
        exec_file_is(fpath)
        execfile(preparse_file_named(fpath), globals)
    else:
        # Preparse in memory only for speed.
        exec_file_is(fpath)
        exec preparse_file(open(fpath).read()) + "\n" in globals