ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 18 Jan 2017 14:30:26 +0100problems with sage_eval and evalhttps://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/Dear all,
I am trying to construct and evaluate sage code, but what used to work in previous versions does not seem to work in SageMath 7.3 any more. Apparently `eval('opr(16*a^2*x^2)')` does not pre-parse the exponent properly:
var('a x')
term1 = 16*a^2*x^2
print (opr(term1))
print eval('opr(term1)')
print eval('opr(16*a^2*x^2)')
gives:
16*a^2*x^2
16*a^2*x^2
RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
In contrast, if I use `sage_eval`, I get a different error:
term1 = 16*a^2*x^2
print (opr(term1))
print sage_eval('opr(term1)')
print sage_eval('opr(16*a^2*x^2)')
gives:
16*a^2*x^2
NameError: name 'opr' is not defined
What would be the correct way of evaluating a text string in a similar way as if it had been entered directly into a code cell?
stanWed, 18 Jan 2017 14:30:26 +0100https://ask.sagemath.org/question/36337/Errors with sage_evalhttps://ask.sagemath.org/question/25461/errors-with-sage_eval/ Hello!
I try to use function <code>sage_eval</code> and It seems for me that either I found bug either I don't understand smth.
cmds = """
def somef():
return 443
res = somef()"""
sage_eval('res', cmds = cmds)
NameError: global name '_sage_const_443' is not defined
sage_eval('res', cmds="def somef(): return 443; res=somef()")
NameError: name 'res' is not defined.
Please give me some hints how to solve this problem. Thanks in advance.
Best regards, Andrei.AndreWinSat, 10 Jan 2015 09:26:01 +0100https://ask.sagemath.org/question/25461/sage_eval: invalid syntaxhttps://ask.sagemath.org/question/9931/sage_eval-invalid-syntax/Hello!
I try in Sage 5.7 Notebook (localhost, not sagenb.org):
sage_eval("y=3")
...
y=Integer(3)
^
SyntaxError: invalid syntax
What's wrong? Thanks.
**Upd**
sage_eval(("y=3", "y"))
3
y
....
NameError: name 'y' is not defined
I want y was defined. What should I do? Thanks.AndreWinThu, 21 Mar 2013 16:00:24 +0100https://ask.sagemath.org/question/9931/