On 1/17/12 4:54 AM, Keshav Kini wrote:
On Tuesday, January 17, 2012 6:06:25 PM UTC+8, luisfe wrote:

    On Jan 16, 5:53 pm, Ed Scheinerman <edward.sc...@gmail.com>
    wrote:
     > I'm confused by the fact that variables defined inside functions can
     > "leak out" and become global variables. Here's what I've noticed.

    The problem is twith the function var. According to its documentation:
    (var?)

    "The new variable is both returned and automatically injected
    into the global namespace. If you need symbolic variable in
    library code, it is better to use either SR.var() or SR.symbol()."

    The var function is only intended to be used on an interactive session
    by the user. If you need variables inside a function you need SR.var


Yes, and I've always wondered why this is. How does it make anything
easier or clearer or better for the interactive user that var() injects
things into global scope? We found ourselves trying in vain to explain
this to students several times when teaching our Sage-based
undergraduate course last fall. (CCing to sage-devel, assuming I did
this right...)


You can do this:

var('x')

instead of this:

x=var('x')

That's less error-prone (you don't have to type/mistype the variable twice), easier to type, etc.

Thanks,

Jason

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

Reply via email to