Dear Stan
On Aug 25, 11:59 am, Stan Schymanski <
schym...@gmail.com> wrote:
> I wondered how python handles assigned variables in function
> definitions.
As much as i understood a recent thread on sage-devel, several people
would like to have a powerful substitution mechanism in Sage.
Concerning your problem: In the definition of h, you use
y.subs(locals()). But i think this can not work, because locals()
refers to the current name space (or what is the word for it?). So,
unless you define a and b *inside* the definition of h, it wouldn't
work.
However, you define a and b in the *global* name space. So, the
following does work:
sage: var('a b x')
(a, b, x)
sage: y=a*x^2+b*x
sage: def h(x):
....: return y.subs(globals())
....:
sage: h(x)
a*x^2 + b*x
sage: a=2
sage: h(x)
2*x^2 + b*x
sage: b=3
sage: h(x)
2*x^2 + 3*x
Cheers
Simon