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
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
10 votes
1 answer
666 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
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
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
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
  • 144k
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,052
23 votes
2 answers
849 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
13 votes
1 answer
956 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
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
9 votes
2 answers
312 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
631 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
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
605 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
16 votes
2 answers
405 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
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
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
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
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
15 votes
1 answer
450 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
5 votes
3 answers
511 views

How does Needs["foo`"] find the file that defines context "foo`"?

The post's title says it all, but if more detail is needed: Suppose I have the following in some file /path/to/foo.m: ...
kjo's user avatar
  • 11.7k
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
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
9 votes
6 answers
944 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?
7 votes
1 answer
174 views

Update/refresh notebook variable color on clear

By default, Mathematica displays variable names already assigned as black while new variables are displayed in blue. When executing Clear["Global`*"], variables ...
egwene sedai's user avatar
  • 2,355
7 votes
3 answers
644 views

Scope within Context

To define some package-wide functions/variables, what approach is recommended? I tried to use contexts with the Begin function, but this failed: ...
Karsten W.'s user avatar
  • 1,383
7 votes
3 answers
253 views

How can I pass replacement rules to a package-defined function and have it understand their context?

I am writing a package where I would like to be able to pass replacement rules to a function inside the package and have the function manipulate its symbols using those replacement rules. However, I'm ...
Emilio Pisanty's user avatar
6 votes
2 answers
474 views

Contexts and parallelization

This question is related to my previous one here. I opened a new question because I think that the root cause is different though. BTW I use MMA 9 on a mac OS X 10.11 (El Capitan). I put here a ...
Yair M's user avatar
  • 601
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,533
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
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,640
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
8 votes
1 answer
141 views

What Does Compile`SetIterate Do?

The list of compilable functions (given by Compile`CompilerFunctions[] // Sort) shows one of the more interesting- and core-sounding functions: ...
Just Some Old Man's user avatar
6 votes
2 answers
153 views

Operators without built-in meaning are incompatible with parallel evaluation

Mathematica has a useful set of Operators Without Built-In Meanings. For example: CenterDot, CircleDot, ...
Ray Shadow's user avatar
  • 7,826
6 votes
1 answer
254 views

Naming conflicts with Dummy variables in several packages conflicts

I'm using several packages in a notebook. In each packages there is functions defined with dummy variables. When I load the several packages in my session, i've got a warning message: ...
sekisushai's user avatar
3 votes
1 answer
116 views

Behavior of Remove inside a Package

I have a notebook containing (in a Code cell, Initialization Group) ...
mitochondrial's user avatar
1 vote
2 answers
145 views

How to keep symbol bindings local to a Notebook section?

I'm using the Notebook on Wolfram-Cloud as a ... well ... notebook to keep notes about the topic I'm studying and to answer exercises. I usually keep one notebook file per chapter, with course notes ...
Sylvain Leroux's user avatar
20 votes
2 answers
399 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
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
716 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
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
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
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
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
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
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
8 votes
1 answer
342 views

Can a package append its context to $DistributedContexts?

I am writing a package (for reference, this one) which I use extensively in parallel computations, and to lighten the coding load on the 'run-time' notebook, I want my package to sit on the ...
Emilio Pisanty's user avatar