No. You can try

def f():
    var('whatever')
f()
print whatever

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 variable under the same name

Some more examples:

t = 5
 -> creates a Python variable named t which contains 5

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

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')

Vincent

On 21/12/15 14:35, Carl Eberhart wrote:
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

On Mon, Dec 21, 2015 at 9:49 AM, Jeroen Demeyer <jdeme...@cage.ugent.be>
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+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.



--
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