To avoid confusion use "symbol" instead of "symbolic variable". A Python variable is a variable in the sense of programming

https://en.wikipedia.org/wiki/Variable_%28computer_science%29

A symbol represents a mathematical variable as in

https://en.wikipedia.org/wiki/Variable_%28mathematics%29

The following is some Python code that makes available a (Python) variable outside of a function

def f():
    global a
    a = 3
f()
print a

But there is *no* such thing as local variable definition in Python. The command "a = 5" or "t = cos(10.)" is in itself a variable declaration... which is a bit confusing if you start mixing with symbols and the terrible var function in Sage. You might have a look at other examples in

http://www.python-course.eu/python3_global_vs_local_variables.php

On 21/12/15 21:12, Carl Eberhart wrote:
Thanks
I guess I don't understand the difference between a python variable and a
symbolic variable.
I know that var is part of Sage but not defined in python.
I also know that the variables created inside a sage procedure using SR.var
are local, but ones created by var are not.
So inside of procedures, use SR.var, not var, unless you want them to be
available outside the procedure.
Is that correct Jeroen?




On Monday, December 21, 2015 at 5:09:05 PM UTC-6, vdelecroix wrote:

No. You can try
Still
def f():
      var('whatever')
f()
print whatever StillThanks.
SR.var  does the trick for me inside of procedures.  var doesn't

Nothing to do with the fact that 't' was globally available before.

By design the function var:
    - creates a new symbolic variable (*not* a Python variable)
    - makes it available in the global namespace as a Python variThanks.
SR.var  does the trick for me inside of procedures.  var doesn'table
under the same namStille

Some more examples:

t = 5 Still
   -> creates a Python variable named t which contains 5
Still
t = SR.var('x')
   -> creates a Python variable named t which contains a symbolic
variable named x Still

var('t')
   -> creates a Python variable named t which contains a symbolic
variable named t.

Indeed the latter should really be thought as

t = SR.var('t') StillThanks.
SR.var  does the trick for me inside of procedures.  var doesn't

Vincent

On 21/12/15 14:35, Carl Eberhart wrote: Thanks.
SR.var  does the trick for me inside of procedures.  var doesn't
Ah.  Thanks very much for that clarification.
Actually, my snippet illustrates the dilemma I was in.
t already has a value outside of f
executing f changes the value of t outside of f
that is what I would expect to happen if t were declared global in f,
but I
thought t was local in f
I still love var, but now I know when to use SR.var instead
Carl Thanks.
SR.var  does the trick for me inside of procedures.  var doesn't

On Mon, Dec 21, 2015 at 9:49 AM, Jeroen Demeyer <jdem...@cage.ugent.be
<javascript:>>
wrote:

On 2015-12-21 16:38, Carl Eberhart wrote:

I admit I don't understand what is happening in the following snippit:

def f():
       t=var('t')
       t=5
       a=2*t
       return a


Solution: never use var() in a function. If you do need a symbolic
variable in a function (note that you don't in the snippet above), you
can
use SR.var() instead of plain var(). That behaves like var(), except
that
it does not change any global. Example:

sage: SR.var('y')
y
sage: y
NameError: name 'y' is not defined

You can use it with explicit assignment:

sage: y = SR.var('y')
sage: y
y


--
You received this message because you are subscribed to a topic in the
Google Groups "sage-support" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/sage-support/G0vP7kulENg/unsubscribe.

To unsubscribe from this group and all its topics, send an email to
sage-support...@googlegroups.com <javascript:>.
To post to this group, send email to sage-s...@googlegroups.com
<javascript:>.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.





--
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to