Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It is not simple to understand the question.

Is for instance the following code injecting a function?

from sage.misc.misc import inject_variable
inject_variable( 'foo', lambda x, y: "%s is entering %s's bar" % ( x, y ) )
foo( 'Fangoraro', 'Harry' )

Result:

"Fangoraro is entering Harry's bar"

Also, one can inject many variables by using a fixed prefix and a numbered running suffix, e.g.

sage: for count in [1..999]:
....:     digits = count.digits( padto=3 )
....:     digits . reverse()
....:     inject_variable( "sol_%s" % ( ''.join( [str(d) for d in digits ] ) ), count^3 )
....:

This initializes a lot of variables, among them:

sage: sol_123
1860867
sage: sol_001
1
sage: sol_002
8

But why not use dictionaries instead? In fact, solve may even give the result in form of a dictionary, just use solution_dict=True. (And merge together all solution dictionaries with different keys. And keys may even be numbers, strings, tuples, or other immutable objects.)