===================================================================
I'd like to start out by saying that I am really glad to see that
you are taking the time to ask all of these questions about contexts.
I suspect that there are a lot of other people who have the same
questions.
Unless I miss my guess, the behavior that you reported is more
closely related to the way that multi-line input is handled, rather
than to contexts.
My guess is that you entered
BeginPackage["test`"];
Begin["Private`"];
y1 = 99;
Print[Context[y1]];
End[];
EndPackage[];
into an Version 3.0 input cell using StandardForm (the default) as
the FormatType.
In a StandardForm input cell, the input above will be processed
as one single input. It is not processed one line at a time. The
same is true of TraditionalForm. In InputForm, this input will be
processed one line at a time.
The first step in processing input is to scan the input for symbols
and other syntax, and construct an expression.
The next step is to evaluate the expression.
Symbols are created in the first step.
BeginPackage["test`"] and Begin["Private`"] are evaluated in the
second step.
You can probably figure out the rest. When the input is processed
all at once, all of the symbols are found or created using the
current settings for $Context and $ContextPath. The symbol y1 in
your input is created in Global` context, since that is the context
that happens to be current when the input is scanned for symbols. It
is only later than BeginPackage["test`"] and Begin["Private`"] are
evaluated, but by then it is too later. The symbol y1 has already
been created in Global` context.
If you enter these same lines into an input cell for which the FormatType
is InputForm, or if you get rid of some of the semicolons, then these
lines will be processed one at a time, or at least the lines that
contain BeginPackage["test`"] and Begin["Private`"] will be processed
before the line that creates y1, and you will get the behavior that
you expected.
I haven't thought about this carefully enough to decide whether or not
I like this behavior. At first sight it seems a little quirky, but to
be fair, I'll need to consider the implications of alternate designs
before making any qualitative comments. When I first saw this, however,
it did take me a while to figure it out, and I am nominally one of the
world's experts on this stuff.
Dave Withoff
Wolfram Research