Global variables called in a function

1,355 views
Skip to first unread message

Walker

unread,
Sep 29, 2010, 10:42:07 AM9/29/10
to sage-support
Hi everybody.
I'm experiencing problems with global variables in Sage. In
particular, I noticed that if I create a global variable, that one is
known everywhere and it's possible to call it everywhere. If I make an
assignment to a variable with the same name of the global one, but
inside a function in another file, it automatically creates a local
variable and makes the global one unknown inside the function itself,
but only outside it. My question is: is there a way to make Sage not
creating a global variable but assigning directly the global one?
Thanks a lot.

Simon King

unread,
Sep 29, 2010, 11:18:41 AM9/29/10
to sage-support
Hi Walker!

On 29 Sep., 16:42, Walker <ebwal...@gmail.com> wrote:
> ... My question is: is there a way to make Sage not
> creating a global variable but assigning directly the global one?

This is actually a Python question. It would of course be very
dangerous if variables defined outside a function would influence what
happens inside a function. 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

Cheers,
Simon

Justin C. Walker

unread,
Sep 29, 2010, 11:21:27 AM9/29/10
to sage-s...@googlegroups.com

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

Walker

unread,
Sep 29, 2010, 12:09:41 PM9/29/10
to sage-support
It seems it has solved my issue, many thanks all of you; I'll attach a
snipped of code next time.
I knew Sage is based on Pyton, but what I don't know is where "the
first ends and the second begins", so I usually think my issue is a
Sage's one...
Anyway, thank you: it was helpful.

Robert Bradshaw

unread,
Sep 29, 2010, 12:26:05 PM9/29/10
to sage-s...@googlegroups.com
On Wed, Sep 29, 2010 at 8:18 AM, Simon King <simon...@nuigalway.ie> wrote:
> Hi Walker!
>
> On 29 Sep., 16:42, Walker <ebwal...@gmail.com> wrote:
>> ... My question is: is there a way to make Sage not
>> creating a global variable but assigning directly the global one?
>
> This is actually a Python question.

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

Walker

unread,
Sep 30, 2010, 6:07:42 AM9/30/10
to sage-support
> 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')

Yes it's true, that's the behavior I was referring to. My problem was
actually that I couldn't print a global variable inside a function
before I made an assignment to it; the error was something like
"Cannot istantiate a local variable before assigning it." and I didn't
understand why I had to assign locally a global variable which had
already been assigned globally. Anyway the keyword "global" solved my
problem.

Robert Bradshaw

unread,
Sep 30, 2010, 12:06:21 PM9/30/10
to sage-s...@googlegroups.com

Yep, a variable is either local or global throughout the entire function.

- Robert

Carl Eberhart

unread,
Dec 21, 2015, 10:38:20 AM12/21/15
to sage-support
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?
Thanks  Carl

William Stein

unread,
Dec 21, 2015, 10:40:46 AM12/21/15
to sage-s...@googlegroups.com


On Monday, December 21, 2015, Carl Eberhart <carl.e...@gmail.com> 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
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?


It is definitely as intended and documented and not a bug.See the docs of var.   Whether or not that design decision (by me from 2007) was a good idea is less clear.     

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


--
Sent from my massive iPhone 6 plus.

Jeroen Demeyer

unread,
Dec 21, 2015, 10:49:11 AM12/21/15
to sage-s...@googlegroups.com
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

Carl Eberhart

unread,
Dec 21, 2015, 12:35:27 PM12/21/15
to sage-s...@googlegroups.com
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



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

Vincent Delecroix

unread,
Dec 21, 2015, 6:09:05 PM12/21/15
to sage-s...@googlegroups.com
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

Carl Eberhart

unread,
Dec 21, 2015, 7:11:23 PM12/21/15
to sage-support
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

Carl Eberhart

unread,
Dec 21, 2015, 7:12:46 PM12/21/15
to sage-support
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
>

Vincent Delecroix

unread,
Dec 21, 2015, 9:01:11 PM12/21/15
to sage-s...@googlegroups.com
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
>> <javascript:>>
>>>> sage-support...@googlegroups.com <javascript:>.
>>>> To post to this group, send email to sage-s...@googlegroups.com
>> <javascript:>.

Jeroen Demeyer

unread,
Dec 22, 2015, 2:54:22 AM12/22/15
to sage-s...@googlegroups.com
On 2015-12-22 01:11, Carl Eberhart wrote:
> Thanks
> I guess I don't understand the difference between a python variable and
> a symbolic variable.

A symbolic variable is a symbol or letter in the mathematical sense. It
is like the "x" appearing in mathematical formulas like
sin(x)^2 + cos(x)^2 == 1.
Reply all
Reply to author
Forward
0 new messages