Tag Info

Hot answers tagged

14

The definitions aren't being lost, they're being shadowed, as described in the tutorial on contexts. Mathematica doesn't warn you about this because it only warns when there is shadowing between contexts that are listed in the $ContextPath. Since Begin only changes $Context and not $ContextPath, you don't get a warning when the symbol that causes shadowing ...


8

I think the symbols sym and $m7res are created by Information. They are not present when the kernel is started. Fresh kernel 1: Quit[] Names["Global`*"] (* {} *) Fresh kernel 2: Quit[] foo = Trace[ Information["Global`*"], TraceInternal -> True]; foo[[8, 3, 5, 7, 2, 9, 18, 65, 2, 1, 3, 6, 7, 5, 6, 4, 3, 3, 6, 2, 8, 12, 10, 5, 6, 2, ...


8

Indeed confusing, but can be explained. Please read here first on how Mathematica searches contexts for symbols. In short, $Context tells it where to create symbols. It's for creation, not for lookup. $ContextPath tells it where to look for symbols but doesn't affect symbol creation. If the symbol is not found in any of the $ContextPath contexts, ...


5

We can replace something, in all definitions associated with a symbol, using function like this: ClearAll[replaceExtendedDefinition] SetAttributes[replaceExtendedDefinition, HoldFirst] replaceExtendedDefinition[sym_, rules_] := Replace[ Language`ExtendedDefinition[sym] , (rule:Rule | RuleDelayed)[lhs_, rhs_] :> ...


4

Note: shown below is an answer to the first version of the OP's question Here is a simple example using scoping constructs, namely Block in this case. The idea is that you can indicate symbols to make local to Block, which implements dynamic scoping: take a look at the "Background and Context" section of its docs for a more complete explanation. Symbols ...


4

As @MarcoB states, you should probably use scoping constructs... If you're opposed to that idea, you can set the Notebooks default context to be Unique to Each Cell Group. I wouldn't recommend that, but it works. You can set that under Evaluation > Notebook's Default Context > Unique to Each Cell Group: Note, to escape this, you need to specify ...


3

This is messy and imperfect, but it will work in simple cases: Import the package contents: pack = ImportString[data, {"Package", "HeldExpressions"}] (* {HoldComplete[BeginPackage["Test`"]], HoldComplete[testFunction::usage = "-";], HoldComplete[Begin["`Private`"]], HoldComplete[testFunction[] := {123, explicitvalue, Hold[explicitvalue]};], ...



Only top voted, non community-wiki answers of a minimum length are eligible