Ask Your Question
1

evaluating a string with variable assignments

asked 2024-08-29 06:02:40 +0100

Max Alekseyev gravatar image

updated 2024-08-29 06:03:06 +0100

Suppose I have a long string of Sage code that performs some variable assignment. I'd like to evaluate it so that those variables become defined in the Sage session. For the sake of simplicity, let it be:

cmd = 'a=1;b=2;'
sage_eval('None',cmds=cmd)
print(a)

This particular code evaluates cmd successfully but the variable values are lost upon completion of sage_eval, which is not what I want. Is it possible to evaluate cmd with keeping all variables assigned?

PS. I know a workaround with a,b = sage_eval('a,b',cmds=cmd) but I'd like to avoid explicit communication of variable values.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2024-08-29 08:36:51 +0100

Emmanuel Charpentier gravatar image

Well...

sage: cmds=('a=1 ; b=2;')
sage: exec(cmds)
sage: a
1
sage: b
2

BTW, you may try a, b = (1, 2);

HTH,

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2024-08-29 06:02:40 +0100

Seen: 720 times

Last updated: Aug 29