Skip to main content

Questions tagged [contexts]

Questions about contexts, which are used in Mathematica to organize defined symbols and avoid conflict between symbols that would otherwise have the same name. They are created when packages are loaded.

Filter by
Sorted by
Tagged with
125 votes
7 answers
7k views

What is the most convenient way to read definitions of in-memory symbols when we don't have the source files? (Spelunking tools)

Note: I put Simon's implementation on GitHub. Contributions welcome! When trying to read the definition of already defined (package or built-in) symbols using ...
Szabolcs's user avatar
  • 235k
51 votes
1 answer
2k views

Local variables in Module leak into the Global context

From the Mathematica documentation on Module: Module allows you to set up local variables with names that are local to the module. But after using ...
owler's user avatar
  • 613
42 votes
7 answers
2k views

How symbol lookup actually works

Bug introduced in V6 and fixed in V11.3 The behavior indeed changed but now the documentation is clear about it. This code is inconsistent with the description from Power Programming with ...
user12753's user avatar
  • 423
38 votes
3 answers
2k views

How to load a package without naming conflicts?

This question applies to any package, but I encountered this problem while working with graphs. There are symbols in the Combinatorica package (such as ...
a06e's user avatar
  • 11.4k
37 votes
3 answers
804 views

How can I evaluate untrusted Mathematica code in a sandbox?

Is there any way to build a sandbox to evaluate untrusted Mathematica expressions in order to prevent them from having (malicious or accidental) harmful side effects? Context: I'm developing a system ...
ibeatty's user avatar
  • 2,543
32 votes
2 answers
1k views

How to make Mathematica variables declarative instead of just-in-time?

Is there a way to have Mathematica at the notebook level (SetOptions[EvaluationNotebook[], CellContext -> Notebook]) only uses a set of declared variables in the ...
Edmund's user avatar
  • 42.3k
28 votes
1 answer
7k views

Why are some function names red?

I need to evaluate Needs["GraphUtilities`"] before I can use functions such as GraphPath[]. When I do so, some functions get ...
CHM's user avatar
  • 3,415
23 votes
2 answers
613 views

Second argument of BeginPackage with nested package loading

I naively thought that the second argument of BeginPackage can simply be used to ensure the loading and availability in the ...
Szabolcs's user avatar
  • 235k
23 votes
2 answers
852 views

Context unique to each group at a specified level

Are there any new tools in v10 that might be applicable to this problem? Recent versions of Mathematica provide the option of having a unique $Context for each ...
Mr.Wizard's user avatar
  • 272k
20 votes
3 answers
1k views

How does $CellContext` work?

The only official info about $CellContext` I was able to find is: placeholder for the context of a symbol inside Dynamic ...
Kuba's user avatar
  • 137k
20 votes
2 answers
401 views

How to find the package file that stores a certain symbol?

Is it possible to figure out in what file a certain packaged symbol lives in (assuming there is no shadowing between multiple identical symbol names)? I would like to have a function that if, given a ...
István Zachar's user avatar
19 votes
2 answers
311 views

How to find out what files are touched when Get is called?

Is there a way to capture all package files touched whenever a package is called via Get, Needs, ...
István Zachar's user avatar
18 votes
1 answer
686 views

Is it possible to use Begin and End inside a Manipulate?

May be someone can explain why I can't use Begin["context`"] and End[] inside a Manipulate ...
Nasser's user avatar
  • 145k
18 votes
4 answers
2k views

Strategies for avoiding and handling shadowing

When dealing with namespaces in Mathematica (BeginPackage, $ContextPath ...) one is unavoidably confronted with the problem of ...
Albert Retey's user avatar
  • 23.6k
16 votes
3 answers
630 views

For any symbol, how can I get the full context-qualified name of the symbol as a string?

I'd like to have a function, whether built-in or not, that when given a symbol, whether bound to a value or not, returns a string giving the context and name of the symbol. Example: ...
Joel Klein's user avatar
  • 5,225
16 votes
2 answers
511 views

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

I'm getting unexpected behavior when beginning a new context, leaving it, and then re-entering it. Specifically, when re-entering the context, symbols that had been previously defined in that context ...
ibeatty's user avatar
  • 2,543
16 votes
1 answer
647 views

Importing an mx file into a context

Consider the following simple code which dumps the global context: var = 5; DumpSave["test.mx", "Global`"]; Now, in a new notebook, I would like to import var in ...
Massimo Ortolano's user avatar
16 votes
2 answers
407 views

Global context seems to be shared between kernels when using Clear?

I am trying to understand the following behaviour, and determine if it's a bug in Mathematica 10. I am using Mathematica 10.3, and can't reproduce this behaviour under Mathematica 9. The test outlined ...
JonathanU's user avatar
  • 161
15 votes
1 answer
451 views

Import package with correct symbol contexts

I am looking to do some automated analysis on packages, e.g. automatically check for common mistakes. Mathematica makes it relatively easy to manipulate code as data, and ...
Szabolcs's user avatar
  • 235k
15 votes
2 answers
324 views

Globally loading packages while using a CellGroup as the default context

I have a notebook I wish to set up where the sections are conceptually related to each other. But, I wish to isolate them from each other programmatically, so that they do not interfere with one ...
rcollyer's user avatar
  • 34k
15 votes
1 answer
418 views

Potential pollution of Global` context in fresh kernel

Bug introduced in 10.0.0 and fixed in 10.0.1 The bug is also present in 12.2.0 and resolved in 12.3.0 on Mac OS X x86 (64-bit). If I evaluate, with a fresh kernel ...
Jacob Akkerboom's user avatar
14 votes
1 answer
325 views

Global` context not empty at startup in v10.2.0

Bug introduced in 10.2 and persisting through 10.2 or later Note that the analysis of this problem revealed a different problem than in Potential pollution of Global` context in fresh kernel (see ...
Chen Stats Yu's user avatar
14 votes
1 answer
719 views

How to automatically do a computation, Quit[], restart and do a new computation

Related questions have been asked before (eg Where does a package have to be loaded?, https://stackoverflow.com/questions/6152023/quit-kernel-in-mathematica-by-a-command-not-using-evaluation-menu, ...
Gaius's user avatar
  • 565
13 votes
4 answers
1k views

What is the accessibility of a package's `Private` context variables?

I've been reading up on how Mathematica handles contexts, $Context, $ContextPath, and a few of the tutorials they have on ...
sgdsgyhetwaraw's user avatar
13 votes
1 answer
957 views

Context of localised (dynamic) symbols

When you localise a symbol with Module, the created unique symbol is usually in the current context. There seems to be an exception to this which has further ...
halirutan's user avatar
  • 113k
13 votes
1 answer
194 views

Is there a way to find out who is importing a package?

After calling some functions, if I look into $ContextPath, I can see that JLink` is in there. Is there a way to find out what ...
Meh's user avatar
  • 1,637
12 votes
2 answers
356 views

How to abbreviate context names?

It's convenient to abbreviate often-used function names, eg Dim = Dimension. Is it possible to abbreviate long context symbols, e.g. Combinatorica when the context ...
alancalvitti's user avatar
  • 15.1k
12 votes
3 answers
340 views

Creating a custom context with a Hold construct

I'm trying to create a function "InContext" to execute a piece of code inside a certain context (and to return the result of evaluating that piece of code). One approach is to place the code inside a ...
Labareda's user avatar
  • 121
12 votes
1 answer
6k views

How do I create and use Mathematica packages?

I have created a package following the tutorial in Mathematica 8 virtual book. However this tutorial is quite, let's call it, "short". I have a TestPackage.m file: ...
Misery's user avatar
  • 2,650
12 votes
1 answer
1k views

How to make Mathematica automatically find packages in subfolders of the Applications directory?

Consider our package awesomepackage.m. If I put this file in FileNameJoin@{$UserBaseDirectory, "Applications"} I can easily ...
glS's user avatar
  • 7,623
12 votes
2 answers
280 views

Why is a symbol still found after changing its context?

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 ...
Szabolcs's user avatar
  • 235k
12 votes
0 answers
233 views

Autocomplete of non-global functions

In Mathematica 9 and 10 they added autocompletion. Out of the box this works great, but when I set the CellContext to "Notebook" ...
Anders's user avatar
  • 121
11 votes
2 answers
339 views

What are the purposes of functions in FrontEnd` context which are duplicates of those in System`?

I noticed that for notebook-related heads, there are usually a pair of same functions, one in System context, another in FrontEnd...
Naitree's user avatar
  • 1,235
11 votes
1 answer
191 views

Attempts at backwards compatibility

I have written a package in Mathematica 11, and I would like to provide some backwards compatibility to earlier versions. An example is the function Echo. ...
Jojo's user avatar
  • 1,278
11 votes
1 answer
252 views

Future-proofing access to packed array tools

Most of the Mathematica users beyond beginner level will be aware of the phenomenon of Packed Arrays. The user-accessible functions related to packed arrays live in the ...
Sjoerd C. de Vries's user avatar
11 votes
1 answer
274 views

NotebookEvaluate breaks notebook's context

Bug introduced in 10.4.1 or earlier and persists through 12.0 A support case with the identification [CASE:3813108] was created [...] I have forwarded an incident report to our developers with the ...
Kuba's user avatar
  • 137k
10 votes
2 answers
1k views

How to properly handle mutual imports of multiple packages?

I'm new to the mathematica platform and I am developing a simulation framework as my first exercise and have been tearing my hair figuring how to make packages interact with each other. Ill explain a ...
Martin Perez's user avatar
10 votes
3 answers
382 views

Functions that creates symbol in a specific context

I'm trying to make a function f that takes a symbol, a value, and a context, and creates the symbol in the requested context and assigns it the passed value. The ...
Mohammed AlQuraishi's user avatar
10 votes
1 answer
667 views

Package functions and symbolic calculations

I'm writing my package (.m) in Mathematica and when I call the function it returns 0 instead of a right output. Here's my code: Package.m ...
Motosega's user avatar
  • 121
10 votes
2 answers
126 views

Functions in a different context are replaced with infix forms of their namesake in System`

So, in an attempt to help out Fabian with his question, I did what I often do, and created a new symbol in the Pillsy` package that I could play with. Since it was ...
Pillsy's user avatar
  • 18.5k
10 votes
1 answer
311 views

What is the purpose of Dump contexts?

Motivation I've been trolling through some internal code, trying to glean design practices WRI developers employ when extending Mathematica using top-level code. During my ...
telefunkenvf14's user avatar
10 votes
0 answers
285 views

The introduction for Context in Wagner's book is out of date? [duplicate]

In Section 8.1.1 (P230 of the book, P249 of the PDF) of Wagner's book, the following example is shown: x = 5; temp`x = 6; Begin["temp`"]; {x, Global`x} The ...
xzczd's user avatar
  • 66.7k
9 votes
6 answers
949 views

How can you give a Module a context and have its local variables and Modules belong to that context?

What would be the best way to give a module its own context and have its local variables and Modules belong to that context?
9 votes
2 answers
257 views

How to define UpValues in a different context from within a package

I'm trying to create a function (in a package) to import a lot of data and create a symbol with UpValues to return specific results about the data. I've got it working fine in a regular notebook, but ...
jlmr's user avatar
  • 155
9 votes
2 answers
314 views

FullForm with context for each symbol?

FullForm strips the context for any symbol that can be found on the Append[$ContextPath, $Context]: ...
masterxilo's user avatar
  • 5,739
9 votes
1 answer
499 views

Where does a package have to be loaded?

I'm trying to solve a very tricky issue in my function. The function needs the NDSolve`FEM` framework. Here is a minimal example: ...
YungHummmma's user avatar
  • 3,082
9 votes
3 answers
925 views

Why doesn't DumpSave work for elements created within a local notebook context?

I recently noticed that when dealing with notebooks with local context, I cannot successfully DumpSave variables, or, rather, when I call ...
shdrums9's user avatar
  • 337
9 votes
1 answer
370 views

What is the correct to have package A use package B in its private section without making B visible to the user?

V 12.1 on windows. I am loading pkg2, which in its Private section loads pkg1 in order to call one of its function. After ...
Nasser's user avatar
  • 145k
9 votes
1 answer
524 views

How to set default for Notebook's Default Context

Every time I create a new notebook, I have to go into the settings and click Evaluation > Notebook's Default Context > Unique to This Notebook. Is there any way to make Mathematica do this ...
Pro Q's user avatar
  • 231
9 votes
1 answer
632 views

Same name for functions in package and Global context

I am using a package that was written for Mathematica 3 while I am now working on Mathematica 8. I have issues with 2 functions in particular, Order and GraphComplement. They are a part of this ...
jasonheush12's user avatar

1
2 3 4 5