First time here? Check out the FAQ!

Ask Your Question
1

evaluating a string with variable assignments

asked 0 years ago

Max Alekseyev gravatar image

updated 0 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
3

answered 0 years ago

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,

Preview: (hide)
link

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: 0 years ago

Seen: 1,058 times

Last updated: Aug 29 '24