Ask Your Question

Revision history [back]

At startup, Sage does not only import things, it also adds a preparser, so that what you typed in Sage is modified on the fly to some valid Python:

sage: type(1/3)
<class 'sage.rings.rational.Rational'>

If we turn the preparser off, we get:

sage: preparser(False)
sage: type(1/3)
<class 'float'>

To see how the preparser modified 1/3, you can do:

sage: preparse('1/3')
'Integer(1)/Integer(3)'

and check:

sage: type(Integer(1)/Integer(3))
<class 'sage.rings.rational.Rational'>

At startup, Sage does not only import things, it also adds a preparser, so that what you typed in Sage is modified on the fly to some valid Python:

sage: type(1/3)
<class 'sage.rings.rational.Rational'>

If we turn the preparser off, we get:

sage: preparser(False)
sage: type(1/3)
<class 'float'>

To see how the preparser modified 1/3, you can do:

sage: preparse('1/3')
'Integer(1)/Integer(3)'

and check:

sage: type(Integer(1)/Integer(3))
<class 'sage.rings.rational.Rational'>

So, to transform some Sage code into a Python module, you have to both add import statements and preparse it.

You could notice that the Sage source code is pure Python, i.e. does not rely on preparser either, maybe looking at it could help to get the idea.