12
$\begingroup$

It seems that we can change the context of symbols:

In[1]:= x = 1
Out[1]= 1

In[2]:= Context[x] = "foo`";

But then x is still interpreted as foo`x (and not Global`x) even though foo` is not in $ContextPath.

In[3]:= x
Out[3]= 1

In[4]:= foo`x
Out[4]= 1

The behaviour is not consistent though. At other times I randomly get this when evaluating the same sequence of expressions in a new kernel:

In[1]:= x = 1
Out[1]= 1

In[2]:= Context[x] = "foo`";

In[3]:= x
Out[3]= x

In[4]:= foo`x
Out[4]= 1

I see this random, erratic behaviour in M9, 10 and 10.1. In M8, x is not seen any more as soon as its context is changed, i.e. it behaves as one would expect.

Questions:

  • Why the random behaviour?
  • Is it documented or discussed anywhere that Context an be changed?
$\endgroup$
2
  • 2
    $\begingroup$ As for 2: I have not seen mentions of this in the docs, but this has certainly been noticed and discussed here on SE before. In particular, I made use of this effect here, where also the comments below that answer refer to earlier discussions regarding this topic. In fact, those comments also mention the first point in your question, although give no answer for it. $\endgroup$ Commented Apr 2, 2015 at 23:35
  • $\begingroup$ I seem to recall a detailed discussion in a question here a year or so ago... will try and find it. Interesting question, +1! $\endgroup$
    – ciao
    Commented Apr 3, 2015 at 0:12

2 Answers 2

5
$\begingroup$

I think this is a caching problem. Considering the examples in the Update documentation it certainly makes sense that this could be the case. Although Update appears to have no effect I have found something that does: Information.

x = 1;
Context[x] = "foo`";

x
foo`x
Context[x]
1

1

"foo`"
?x
foo`x

x=1
x
foo`x
Context[x]
x

1

"Global`"
  • On my system this is reliable; I get the same output every time I run it.

  • Rather surprisingly Information returns the definition for foo`x but after it is used, such as if it is used again, x points to Global`x.

  • It does not seem to matter if x is involved; I can for example replace ?x with ?Plus in the lines above and it behaves the same way.

$\endgroup$
3
  • 1
    $\begingroup$ Interestingly, Update with any invalid argument also seems to work (so Update["hello world"] works, but neither Update[] nor Update[Unevaluated@x] nor Update with any function related to symbols, expressions or context I've tried). $\endgroup$
    – celtschk
    Commented Oct 22, 2015 at 9:36
  • $\begingroup$ @celtschk Interesting observation; thanks for the note! Do you have any hypothesis as to why this works? Do you think it's just random behavior? $\endgroup$
    – Mr.Wizard
    Commented Oct 29, 2015 at 3:04
  • 1
    $\begingroup$ From further tests, it seems that everything generating a message works; not only Update[""] but also 1/0. Anyway, I now noticed something different: My previous tests had all been by directly entering into the kernel; now I tried in the notebook, and there the context assignment seems to work immediately. $\endgroup$
    – celtschk
    Commented Oct 29, 2015 at 19:28
0
$\begingroup$

No erratic behavior on my computer M9. However a simple workaround could be considered that achieves its purpose.

Workaround for moving a symbol from one context to another can be achieved by: For example, create symbol in the context Global and move it to foo`

    x = 1;           (*create x in Global` *)
    foo`x = x;       (*create x in foo` and copy Global`x *)
    Remove @ x       (*remove x from Global` it will only exist in foo`*)
    {x, foo`x}
    (* {x,1} *)

Moving it back to Global`

    x=foo`x;
    Remove @ foo`x
    {x,foo`x}
    (* {1,foo`x} *) 

PS: If you don't know ask the penguins...hi,hi,hi

$\endgroup$
5
  • $\begingroup$ No, this is not a bug, it's an undocumented feature. There are many symbols that behave like this. Begin simply sets $Context and is unrelated to what I was talking about in the question, i.e. changing the context of an existing symbol. "$ContextPath is only for searching for symbols not found within Contexts[]" <-- this is plain incorrect. $\endgroup$
    – Szabolcs
    Commented Apr 3, 2015 at 1:17
  • $\begingroup$ Your are right, I meant $Context instead of Context[], sorry for mistake. I have corrected $\endgroup$
    – penguin77
    Commented Apr 3, 2015 at 10:24
  • $\begingroup$ @ Szabolcs, sorry if haven't understood your question correctly $\endgroup$
    – penguin77
    Commented Apr 3, 2015 at 12:02
  • $\begingroup$ This is a way to copy the definition of variable in very simple cases to a new symbol, but copying is not the same as changing the context of the symbol. But fortunately I do not need this, I was just trying to understand how it works. :-) So why is it not the same? Because x may have been referenced somewhere. Consider a=x. Now a points to Global`x . After Context[x]="foo`", a will point to foo`x. If x is Removed, then this is not the case any more. $\endgroup$
    – Szabolcs
    Commented Apr 3, 2015 at 14:14
  • 4
    $\begingroup$ Another thing to point out that this sort of copying only works in the very simplest of cases. It won't work if x:=1+1. It also won't work if x=b; b=1, or if x has DownValues, Attributes, or other properties ... There's a question about how to reliably copy definitions here. $\endgroup$
    – Szabolcs
    Commented Apr 3, 2015 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.