I'm unclear what you're trying to achieve with the functions. exec()
takes a string of Python code and executes it. The entire string must
be valid Python code by itself, so exec('return x') fails because a
bare "return x" is not valid Python. "return" must be inside a
function definition to be valid. But even defSym1 doesn't do anything
useful beyond just returning Sym1, so there's no point to having it
instead of just "Sym1" directly.

The LaTeX version of Sym1 contains _ because SymPy automatically
assumes that symbol names ending in numbers are subscripted, so it
renders it as a Sym_1 in LaTeX. If you don't want the 1 to be
subscripted, you can use something like Symbol("{Sym1}").

Aaron  Meurer

On Tue, Nov 3, 2020 at 9:42 AM Thomas Ligon <[email protected]> wrote:
>
> The following code is not working as I expected.
> Why does Sym1 contain an underline (_{1})?
> Why does return fail (unhandled  exception) in line 7?
>
> Code:
> import sympy
> from sympy import symbols, latex
> def defSym1():
>     return Sym1
> def defSym2():
>     strSym = 'return Sym2'
>     exec(strSym)
> Sym1 = symbols('Sym1')
> Sym2 = symbols('Sym2')
> print(latex(defSym1()))
> print(latex(defSym2()))
> print('end testSym')
>
> Output:
> Sym_{1}
> 'return' outside function (<string>, line 1)
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/4a1df466-d3a3-4f7b-8a14-5e5a906f2662n%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6J5C0a7fSb413FDMj1U4sKjTB93aMyL9%3DPymoDe5ony1A%40mail.gmail.com.

Reply via email to