3

Starting a fresh kernel and writing the following set of commands:

Define a new context temp and a variable name x in it.

temp`x=2;

Next define a global variable with the same name.

x=1;

Next I run the following commands sequentially and separately so that parser does not use the same context throughout.

Begin["temp`"];

Print[{x, Global`x, temp`x}];

End[];

I get {1, 1, 2} but was expecting {2, 1, 2}.

If I look at $ContextPath, temp doesn't exist in it.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
user13892
  • 9,375
  • 1
  • 13
  • 41
  • Look at the documentation of $ContextPath: "...is a global variable that gives a list of contexts to search, before $Context, in trying to find a symbol that has been entered.". – halirutan Jun 19 '18 at 10:59
  • When I begin temp then shouldn't x correspond to temp`x not Global`x. – user13892 Jun 19 '18 at 11:05
  • What can I say? :) The namespace system of Mathematica is confusing and one of the worst you will ever see in today's languages. It's why Wolfram puts all new functions directly into the System` namespace, because no sane user understands how to work with contexts. Fact is, you can get a pretty decent understanding of how everything works by reading the documentation of Begin, BeginPackage, $Context, and $ContextPath. You will remember this for the rest of ... the next few days. Then you look it up again. Sorry, but unfortunately, that's how it is. – halirutan Jun 19 '18 at 11:13
  • I think linked topic should answer everything except why those decisions were made. Let me know if you disagree with closing. – Kuba Jun 19 '18 at 11:24

0 Answers0