0
$\begingroup$

as the following picture shows, when I Begin[] a same context(same name), why i use the function yjn[](defined in the previous same name context) still have to write the context name?

enter image description here

similar question can be referenced(but still not solve my confusion)

How symbol lookup actually works

Why does leaving and re-entering a context lose values defined in that context?


Ans: Thanks for lericr's attention, the core is that i call yjn[] in Global` which will generate a Global`yjn[], so when i call yjn[] in MyContext` again, now there are two yjn[](how silly am I~). to confirm this thought, i comment the yjn[3] in the Global`, result is I can use short name yjn[] directly in MyContext`.

$\endgroup$

1 Answer 1

3
$\begingroup$

All that Begin does is set the context, which basically just means it sets the automatic naming of new symbols. Every symbol has a fully qualified name that includes the full context. It's just a convenience that this fully qualified name is usually hidden from us.

Importantly, Begin doesn't change $ContextPath. So, when trying to resolve a symbol, it'll use whatever is discovered in $ContextPath.

At input 7, when you evaluated yjn[3], you put a new symbol into the Global' context. Later you used Begin and evaluated yjn[4]. Well, yjn was found in Global', and so that symbol's name is really Global'yjn. Since no definitions are associated to that symbol, you just get yjn[4] back.

To change $ContextPath you need to use BeginPackage and EndPackage.

$\endgroup$

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.