Re: [sage-support] Re: Global variables called in a function

2015-12-21 Thread Vincent Delecroix

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


Re: [sage-support] Re: Global variables called in a function

2015-12-21 Thread Carl Eberhart
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  > 
> > 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 . 
> >> 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 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.


Re: [sage-support] Re: Global variables called in a function

2015-12-21 Thread Carl Eberhart
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  > 
> > 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 . 
> >> 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 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.


Re: [sage-support] Re: Global variables called in a function

2015-12-21 Thread Vincent Delecroix
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 

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


Re: [sage-support] Re: Global variables called in a function

2015-12-21 Thread Jeroen Demeyer

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.

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


Re: [sage-support] Re: Global variables called in a function

2015-12-21 Thread Jeroen Demeyer

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


Re: [sage-support] Re: Global variables called in a function

2015-12-21 Thread William Stein
On Monday, December 21, 2015, 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
> 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.



> Thanks  Carl
> On Thursday, September 30, 2010 at 11:06:21 AM UTC-5, Robert Bradshaw
> wrote:
>>
>> On Thu, Sep 30, 2010 at 3:07 AM, Walker  wrote:
>> >> 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.
>>
>> Yep, a variable is either local or global throughout the entire function.
>>
>> - Robert
>>
>>




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


-- 
Sent from my massive iPhone 6 plus.

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


Re: [sage-support] Re: Global variables called in a function

2015-12-21 Thread Carl Eberhart
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
On Thursday, September 30, 2010 at 11:06:21 AM UTC-5, Robert Bradshaw wrote:
>
> On Thu, Sep 30, 2010 at 3:07 AM, Walker  
> wrote:
> >> 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.
>
> Yep, a variable is either local or global throughout the entire function.
>
> - Robert
>
>

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


Re: [sage-support] Re: Global variables called in a function

2015-12-21 Thread Carl Eberhart
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 
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.


[sage-support] Re: Global variables called in a function

2010-09-30 Thread Walker
 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.

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org


Re: [sage-support] Re: Global variables called in a function

2010-09-30 Thread Robert Bradshaw
On Thu, Sep 30, 2010 at 3:07 AM, Walker ebwal...@gmail.com wrote:
 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.

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

- Robert

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org


[sage-support] Re: Global variables called in a function

2010-09-29 Thread Simon King
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

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org


[sage-support] Re: Global variables called in a function

2010-09-29 Thread Walker
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.

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org


Re: [sage-support] Re: Global variables called in a function

2010-09-29 Thread Robert Bradshaw
On Wed, Sep 29, 2010 at 8:18 AM, Simon King simon.k...@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

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org