If you insist to write code this way I think you should really try to understand what $ContextPath
and $Context
do and how BeginPackage
,EndPackage
, Begin
and End
control these.
As Mr. Wizard has explained the problem you are facing is that within the private part of the package Test1`
is in $ContextPath
, but not Test1`Private`
. If neither Test1`function1
nor Test1`Private`function1
exist, no function1
can be found in $ContextPath
and a new symbol is generated using the current value of $Context
which is Test1`Private`
. After an explicit call to Test1`function1
that symbol exists. When you load the package code after Test1`function1
exists, it will be found in $ContextPath
and will be used for the definition in the private part and no private symbol Test`Private`function1
will be used or generated.
The following will put the private context which $Context
points to before everything else in $ContextPath
, so now the definition will be made to the private symbol even if the public symbol exists. This should solve your problem and doesn't need to make any changes to other symbols, whether they exist or not:
BeginPackage["Test1`"];
Begin["`Private`"];
PrependTo[$ContextPath, $Context];
function1[x_] := x;
End[];
EndPackage[];
You should be aware that such manual manipulations of $ContextPath
might have unexpected side effects, so it should be used with some care. Considering this, it probably would be a good idea to explain more clearly what you try to achieve, my impression is that you are probably abusing the namespace functionality for something that probably could be achieved better with other means.