I do not understand this behavior:
method[args_] :=
(* forget the context path *)
Block[{$ContextPath},
(* should given nothing, right? *)
Print["cpath=", $ContextPath];
...
but when evaluated it gives the full context path (i.e. it does not forgets it). Why is that? This is against everything I (seem to) know about the Block
function.
Answer
If I try this, everything gets removed except for {System`,Global`}
:
In[1]:= $ContextPath
Out[1]= {"PacletManager`", "QuantityUnits`", "WebServices`", "System`", "Global`"}
In[2]:= Block[{$ContextPath}, Print[$ContextPath]]
During evaluation of In[2]:= {System`,Global`}
My guess is that this is a special exception which is implemented to make $ContextPath
usable like this:
Block[{$ContextPath}, Needs["SomePackage`"]]
This loads the package without adding it to the context path. It's quite useful with Combinatorica in version 8 and later.
It is in fact possible to remove System`
and Global`
from $ContextPath
but this just breaks the system completely and would also make it impossible to load any packages. You can (in a new session, where you won't lose work!!) try $ContextPath = {}
and render the system unusable if you are using a notebook interface. The reason why package loading would be broken is that System`
context symbols would need to be referenced by the full name, as in System`Sin
, which packages don't do.
So I think this is a special exception to the behaviour of Block
/$ContextPath
that makes the above use case possible.
Comments
Post a Comment