Remember that Sage uses Python as its underlying language, so (most of) the rules of Python apply here. For most programming questions, the Python documentation will give you a lot of help.
Also, in general, a small snippet of code, showing the problem you are having, along with your request for help can simplify our job in helping you.
In this case, you problem seems pretty clear, and is answered by the Python doc:
In the scope of a procedure, all variables are local, not global, unless explicitly specified. The way to do this is with the 'global' statement, which should list the global variables that you want to access within the scope of your procedure.
Note that if you name a variable in a global statement, and one of that name is not in global scope, it will be created if you assign to it.
Thus the following:
sage: def xx(n):
...: global fred, ralph
...: fred = n
...:
sage: xx(3)
sage: fred
3
However, if you don't assign to a non-existent global variable, and just refer to it, you will get a thrown exception:
sage: def xx(n):
...: global fred, ralph
...: print ralph
...:
sage: xx(3)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/Volumes/Zeppo/SandBox/Justin/sb/Sage/<ipython console> in <module>()
/Volumes/Zeppo/SandBox/Justin/sb/Sage/<string> in xx(n)
NameError: global name 'ralph' is not defined
HTH
Justin
--
Justin C. Walker
Curmudgeon-at-large
Director
Institute for the Absorption of Federal Funds
----
186,000 Miles per Second
Not just a good idea:
it's the law!
----
Yes.
> It would of course be very
> dangerous if variables defined outside a function would influence what
> happens inside a function.
No, that's the expected and useful behavior. Otherwise you couldn't
even call other functions from your function (as they are just
"variables").
> So, unless you explicitly declare *inside
> the function* that a variable is global, it won't be visible inside
> the function.
>
> So, you could do:
> sage: def f():
> ....: global x
> ....: print x
> ....:
> sage: x=3
> sage: f()
> 3
> sage: x=5
> sage: f()
> 5
Your f will have the same behavior even if x is not declared global.
It works just as it does in Python. Global variables are by default
readable but not writeable from the local scope, so if you do an
assignment and don't declare a variable to be global, then a local
shadow will be created. (Function arguments are considered assignments
as well.) In other words, variable declaration in Python is done by
assignment (as opposed to other languages where it is explicit). An
example is worth a thousand words:
sage: x = "this is x"
sage: y = "this is y"
sage: z = "this is z"
sage: def f():
....: print x
....: y = "new value"
....: print y
....: global z
....: z = "new value"
....: print z
....:
sage: f()
this is x
new value
new value
sage: x, y, z
('this is x', 'this is y', 'new value')
- Robert
Yep, a variable is either local or global throughout the entire function.
- Robert
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
t=3;t;f();t
3
10
t
This was executed in a sagews cell
I think that the t which is referenced in f should be a local variable.
However the value of t outside of f is modified by the execution of f.
I know this happens because of the statement t=var('t').
Apparently, varing a variable inside a procedure vars it outside the procedure too.
Is this behavior correct?
--
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...@googlegroups.com.
To post to this group, send email to sage-s...@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 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.
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
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
>