Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Python (hence in Sage), a variable is a label for an object

a=2

makes the label a point to the object 2

x=a

m

In (Very roughly) : in Python (hence in Sage), a (Python) variable is a label for an object

sage: a=2

makes the label a point to the object 2

sage: x=a

m

makes the label x point to the same object as the label a, _i. e._ the object 2

sage: a=3

makes the label a point to the object 3. But x still points to the object 2 :

sage: x
2

Do not confuse Python variables (i. e. labels to objects) with Sage's symbolic variables, which are objects recognized as (mathematical) variable in symbolic expressions :

sage: reset() # Roughly : start again...
sage: x
x

That is how Sage's symbolic variables are printed : by their name, wich, by default, is also given to the (Python) variable

sage: a=x

a is now a Python variable, i. e. a label pointing to the Sage symbolic variable whose name is "x"

sage: z=SR.var("y")
sage: z
y

Indeed : z is a Python variable, i. e. a label for the Sage symbolic variable whose symbolic name is "y".

Furthermore :

sage: y
...
NameError: name 'y' is not defined

Indeed, you have never defined a Python variable named y.

If you want a "delayed assignment", _i. e._ a symbol for something evaluated each time it is used, you have to define either a symbolic function (this must have an argument and can be done but using only symbolic functions) or a Python function (which can be defined with no argument and use any function) :

sage: def a(): return random()
sage: a()
0.9439976532284053
sage: a()
0.7276353240432383

But you have to use the "function call" syntax :

sage: a
<function a at 0x7f555e54d668>

There might be some Pythonistic way to "hide" a function call in a variable name, bit, if so, I do not know it.

A bit more about Python and symbolic variables might come in handy. When you do :

sage: var("t, u, v")

you are, in fact, using a shortcut for :

sage:t, u, v = SR.var("t, u, v")

A cause of further confusion is that, in a "virgin" interpreter, the Python variable x is, by default, a label to the Sage symbolic variable whose symbolic name is "x" (it is the only one in this case...).

This shortcut, supposed to "help beginners", has been grandfathered, but may have trapped quite a lot of not-totally-beginners...

The distinction between Python variables (i. e. computer objects, part of programs) and symbolic variables (i. e. one of the many types of objects programs act, _i. e._ data) is characteristic (some would say pathognomonic ,-) of Sage (and SymPy). This helps programming. Really...

The only problem is that some other systems, such as Maxima or Mathematica, do not do this distinction ; when interacting with such system, they are apt to return to you objects containing variables that have never been declared, and to which no Python variable points to (Maxima's solve and desolve, which Sage uses by default, are chronic offenders...). The Sage user has to to search the names of symbiloc variables present in the returned object(s), and declare those not already known.

HTH,