Anonymous user
Runtime evaluation/In an environment: Difference between revisions
Runtime evaluation/In an environment (view source)
Revision as of 07:55, 18 February 2009
, 15 years ago→{{header|Python}}: swapped introspection for another way to use more than one variable
(→{{header|Python}}: alternate implementation using compile & introspection) |
(→{{header|Python}}: swapped introspection for another way to use more than one variable) |
||
Line 48:
=={{header|Python}}==
<lang python>>>> def eval_with_x(code,
>>> eval_with_x('2 ** x', 3, 5)
24</lang
===Python:
A slight change allows the evaluation to take multiple names:
<lang python>
return eval(code, kwordargs)
>>> code = '2 ** x'
>>> eval_with_args(code, x=5) - eval_with_args(code, x=3)
▲ eval(func, name[1]) - eval(func, name[0])
24
</lang>▼
>>> code = '3 * x + y'
>>> eval_with_args(code, x=5, y=2) - eval_with_args(code, x=3, y=1)
7
▲>>> </lang>
=={{header|Ruby}}==
|