On Thu, Oct 29, 2009 at 2:09 AM, zeliboba <[email protected]> wrote: > > hmm... the question was different: "attach" makes variable global, > "import" does not. is it bug or feature?
Feature. When you use attach, Sage executes the code then and there in the scope of your current file. When use "import", you're using the standard Python import mechanism (which doesn't play so well with autoreloading etc.). Two completely different problems are solved by attach and import. In the Python programming world, one *only* has import, and for writing big programs that is all you need. For interactive work on the command line (or in the notebook), which we do a lot with Sage, of course, then attach turns out to be very useful as well. I added attach to Sage very early on to somewhat mimic the command Attach(...) in Magma, which I had grown fond of. William > > > On Oct 28, 5:52 pm, William Stein <[email protected]> wrote: >> On Wed, Oct 28, 2009 at 5:16 AM, zeliboba <[email protected]> wrote: >> >> > dear all >> >> > in documentation for var() it is stated "variables ... automatically >> > injected into the global namespace". I'd like to define variable in >> > module and then use it from several scripts, but variable is not >> > exported actually. my prog.sage looks like: >> >> > from mod import * >> > createVar('A') >> >> Do: >> >> A = var('A') >> >> >> >> > f = A^2 >> > print f >> >> > and module mod.py : >> >> > from sage.all import * >> > def createVar(s): >> > return var(s) >> >> > it gives: >> >> > $ sage prog.sage >> > Traceback (most recent call last): >> > File "prog.py", line 7, in <module> >> > f = A**_sage_const_2 >> > NameError: name 'A' is not defined >> >> > A is definitely not in global namespace. it only works correctly if I >> > change >> > from mod import * >> > to >> > attach mod.py >> >> > is it intended behavior? >> >> -- >> William Stein >> Associate Professor of Mathematics >> University of Washingtonhttp://wstein.org > > > -- William Stein Associate Professor of Mathematics University of Washington http://wstein.org --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---
