Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I defined a function TestHead in a notebook as follows:

TestHead[z___] := TrueQ[{z} == Cases[{z}, x_ /; Head[x] === avar]]

to test whether a bunch of variables all have a certain head avar. When I do this in a notebook and define a variable a using

a /: Head[a]=avar

this works fine and gives True for TestHead[a]. However, when I make a package

TestHead[z___] := TrueQ[{z}==Cases[{z},x_/;Head[x]===avar]]

load the package and again define a variable a as above, the function TestHead now gives False when acting on a.

Why is this and how I can remedy it?

share|improve this question
up vote 3 down vote accepted

It's because avar is in the TestHead`Private` context. One fix is to write your package with an explicit context on avar, such as Global`:

TestHead[z___] := TrueQ[{z}==Cases[{z},x_/;Head[x]===Global`avar]]

You could have debugged this for instance with Trace and you would have seen instantly, why your SameQ results in False:


share|improve this answer

The other possibility, of course, is to have avar be declared by the package (it will thus be under the TestHead` context, i.e. its full declaration is TestHead`avar):

TestHead::usage = ""
avar::usage = ""
TestHead[z___] := TrueQ[{z} == Cases[{z}, x_ /; Head[x] === avar]]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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