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 <[email protected] <javascript:>>
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.