Wrapping Begin["Context`"], ..., End[] into a function
- To: mathgroup at smc.vnet.net
- Subject: [mg106046] Wrapping Begin["Context`"], ..., End[] into a function
- From: Leo Alekseyev <dnquark at gmail.com>
- Date: Thu, 31 Dec 2009 03:15:46 -0500 (EST)
Dear Mathgroup, I was wondering if it's possible to write a function that evaluates an expression in a particular context. In other words, instead of writing this: cpsave = $ContextPath; Begin["Materials1`"]; $ContextPath = {"Materials1`", "System`"}; myExpression; End[]; $ContextPath = Global`cpsave; I would like to be able to make a function call, e.g. evaluateInContext["Materials1`",myExpression] The naive solution of evaluateInContext[cont_,expr_]:=( cpsave = $ContextPath; Begin[cont]; ... ) doesn't work because any symbol in parentheses is bound to the current context before any evaluation takes place. Is there a trick to make this work? Thanks, and happy New Year to all! --Leo