Mathematica 9 is now available
Student Support Forum
Student Support Forum: 'How can I change context inside a function?' topicStudent Support Forum > General > Archives > "How can I change context inside a function?"

Help | Reply To Topic
Author Comment/Response
02/24/09 12:19pm

How can I change context inside a function? For example:

MyContext`X = 1;
X = 10;
f[] := (
$ContextPath = Prepend[$ContextPath, "MyContext`"];
Print[X, ", ", MyContext`X];
$ContextPath = Delete[$ContextPath, 1];

It gives me:


10, 1

But that is wrong! Inside the function f, the $ContextPath has "MyContext`" in the first place. Therefore both variables X and MyContext`X belong to the context "MyContext`" and should be equal to 1, but Mathematica says that they are different...

URL: ,
Help | Reply To Topic