What is the accessibility of a package's `Private` context variables? Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How symbol lookup actually worksWhat are recommended guidelines for developing packages?How to properly handle mutual imports of multiple packages?How can Private functions be made completely opaque?Does one need to be careful about loading multiple (many) contexts or packages in the same session?WebServices context problemHow to pass rules to packagesA question regarding shadowed symbolsIs there any harm or benefit to Removing unneeded private symbols in packages?Information (??) of function defined in Package return the function with long name of variablesHow to resolve a context shadow problem (revised)

Do any jurisdictions seriously consider reclassifying social media websites as publishers?

What's the meaning of "fortified infraction restraint"?

What would you call this weird metallic apparatus that allows you to lift people?

A term for a woman complaining about things/begging in a cute/childish way

How to install press fit bottom bracket into new frame

What do you call the main part of a joke?

Can a new player join a group only when a new campaign starts?

An adverb for when you're not exaggerating

Why is Nikon 1.4g better when Nikon 1.8g is sharper?

How would a mousetrap for use in space work?

Crossing US/Canada Border for less than 24 hours

Generate an RGB colour grid

How to write the following sign?

Should I follow up with an employee I believe overracted to a mistake I made?

AppleTVs create a chatty alternate WiFi network

Source for Esri sample data from 911 Hot Spot Analysis

Significance of Cersei's obsession with elephants?

Illegal assignment from sObject to Id

Why doesn't SQL Optimizer use my constraint?

Is there hard evidence that the grant peer review system performs significantly better than random?

Using et al. for a last / senior author rather than for a first author

Do I really need to have a message in a novel to appeal to readers?

How to react to hostile behavior from a senior developer?

Why aren't air breathing engines used as small first stages?



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



Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How symbol lookup actually worksWhat are recommended guidelines for developing packages?How to properly handle mutual imports of multiple packages?How can Private functions be made completely opaque?Does one need to be careful about loading multiple (many) contexts or packages in the same session?WebServices context problemHow to pass rules to packagesA question regarding shadowed symbolsIs there any harm or benefit to Removing unneeded private symbols in packages?Information (??) of function defined in Package return the function with long name of variablesHow to resolve a context shadow problem (revised)










10












$begingroup$


I've been reading up on how Mathematica handles contexts, $Context, $ContextPath, and a few of the tutorials they have on Packages.



What I'm wondering about is how the functions defined in, say, CustomPackage` are able to access the variables in CustomPackage`Private`.



For example,



BeginPackage["CustomPackage`"]

MyFunction::usage = "MyFunction[arg1] adds 5 to arg1."

Begin["`Private`"]

abc=5;
MyFunction[arg1_] := arg1 + abc;

End[]
EndPackage[]


When I load the package <<CustomPackage` the $ContextPath will have CustomPackage` on it, but not CustomPackage`Private`



So how does MyFunction know the value of abc at the delayed function call (when it is called) if the Private` context isn't on the $ContextPath










share|improve this question











$endgroup$
















    10












    $begingroup$


    I've been reading up on how Mathematica handles contexts, $Context, $ContextPath, and a few of the tutorials they have on Packages.



    What I'm wondering about is how the functions defined in, say, CustomPackage` are able to access the variables in CustomPackage`Private`.



    For example,



    BeginPackage["CustomPackage`"]

    MyFunction::usage = "MyFunction[arg1] adds 5 to arg1."

    Begin["`Private`"]

    abc=5;
    MyFunction[arg1_] := arg1 + abc;

    End[]
    EndPackage[]


    When I load the package <<CustomPackage` the $ContextPath will have CustomPackage` on it, but not CustomPackage`Private`



    So how does MyFunction know the value of abc at the delayed function call (when it is called) if the Private` context isn't on the $ContextPath










    share|improve this question











    $endgroup$














      10












      10








      10





      $begingroup$


      I've been reading up on how Mathematica handles contexts, $Context, $ContextPath, and a few of the tutorials they have on Packages.



      What I'm wondering about is how the functions defined in, say, CustomPackage` are able to access the variables in CustomPackage`Private`.



      For example,



      BeginPackage["CustomPackage`"]

      MyFunction::usage = "MyFunction[arg1] adds 5 to arg1."

      Begin["`Private`"]

      abc=5;
      MyFunction[arg1_] := arg1 + abc;

      End[]
      EndPackage[]


      When I load the package <<CustomPackage` the $ContextPath will have CustomPackage` on it, but not CustomPackage`Private`



      So how does MyFunction know the value of abc at the delayed function call (when it is called) if the Private` context isn't on the $ContextPath










      share|improve this question











      $endgroup$




      I've been reading up on how Mathematica handles contexts, $Context, $ContextPath, and a few of the tutorials they have on Packages.



      What I'm wondering about is how the functions defined in, say, CustomPackage` are able to access the variables in CustomPackage`Private`.



      For example,



      BeginPackage["CustomPackage`"]

      MyFunction::usage = "MyFunction[arg1] adds 5 to arg1."

      Begin["`Private`"]

      abc=5;
      MyFunction[arg1_] := arg1 + abc;

      End[]
      EndPackage[]


      When I load the package <<CustomPackage` the $ContextPath will have CustomPackage` on it, but not CustomPackage`Private`



      So how does MyFunction know the value of abc at the delayed function call (when it is called) if the Private` context isn't on the $ContextPath







      packages core-language scoping contexts






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 10 at 22:10









      m_goldberg

      88.9k873200




      88.9k873200










      asked Apr 10 at 19:03









      w1resw1res

      23015




      23015




















          4 Answers
          4






          active

          oldest

          votes


















          10












          $begingroup$


          So how does MyFunction know the value of abc at the delayed function call (when it is called) if the Private` context isn't on the $ContextPath




          There is a misunderstanding here. You are assuming that abc is searched for in some context only when MyFunction[something] is evaluated. This is not the case.



          $Context and $ContextPath only affect how source code is parsed (not how expressions are evaluated). In other words, they only affect how the text you write in the package file is interpreted and converted into in-memory expressions. Once the package has been loaded with Get, this interpretation has already happened. MyFunction has been interpreted as the symbol CustomPackage`MyFunction and abc has been interpreted as CustomPackage`Private`abc, according to the value of $Context and $ContextPath at the time each was read. These are the full names of these symbols and this is how they exist in memory.



          Load the package and try this:



          Block[$ContextPath,
          Print@Definition[MyFunction]
          ]


          You'll see the following printed:



          CustomPackage`MyFunction[CustomPackage`Private`arg1_] := 
          CustomPackage`Private`arg1+CustomPackage`Private`abc


          As you can see, a context is always associated with every symbol.






          share|improve this answer











          $endgroup$




















            7












            $begingroup$

            Begin["`Private`"]; sets the current $Context to "CustomPackage `Private`". This causes two things:



            • The symbol abc will be searched in the current context first, thus in"CustomPackage`Private`". Only if it is not found there, the search goes on along $ContextPath.


            • If no matching symbol is found this way, a new symbol abc is created, namely in the current $Context which is "CustomPackage`Private`". So the full symbol name is "CustomPackage`Private`abc".


            For example, running your code in a fresh kernel and executing



            ??MyFunction


            reveals that the full definition of MyFunction is




            MyFunction[CustomPackage`Private`arg1_]:=CustomPackage`Private`arg1+CustomPackage`Private`abc




            Moreover, with



             ?*`abc


            you see that the only symbol in all contexts that matches abc is CustomPackage`Private`abc and has the value 5 assigned to it.






            share|improve this answer











            $endgroup$




















              6












              $begingroup$

              All symbols are created at load time, so when you do:



              BeginPackage["X`"];

              x::usage="Declaring x as an exported symbol in the X` context";

              Begin["`SomePrivateContext`"];

              x[a_]:=b

              End[];

              EndPackage[];


              x was created as X`x but the DownValues of x reference X`SomePrivateContext`a and X`SomePrivateContext`b which were created at the time the function was defined. These symbols are unique, so that reference only ever points that a single object.






              share|improve this answer









              $endgroup$




















                3












                $begingroup$


                So how does MyFunction know the value of abc
                at the delayed function call (when it is called)
                if the Private` context isn't on the $ContextPath?




                because "CustomPackage`Private`" is the value of $Context when MyFunction is defined (i.e. it is not just $ContextPath that determines what a function sees but also what is on $Context).




                TL:DR



                This is a timely question because it indirectly touches upon the competing imperatives of developers and end-users. To the question itself:



                The whole point of packages is that they are a form of encapsulation that allows developers to, without interferance, implement functionality for end-users without bothering them with the underlying details. In particular, the encapsulation involves controlling namespaces so that the underlying details can involve symbols that help implement the functionality but ultimately don't end-up polluting a user's namespace. All symbols defined in a "*`Private`" namespace are created for exactly this purpose.



                Hence in the OP's example, the variable abc is an underlying detail for the implementation of the public MyFunction. The developer needs the "detail" of abc but this particular symbol is of no direct interest to an end-user who typically just ends up calling MyFunction[].



                The package layout achieves this encapsulation by manipulating $ContextPath and $Context as the control-flow passes through the package when it is first loaded. This is described in the other answers and documentation but it can be useful to see it directly:



                loc[n_] := Sow[<|
                "Location" -> n,
                "$Context" -> $Context,
                "$ContextPath" -> $ContextPath|>];

                Reap[

                loc@1;

                BeginPackage["CustomPackage`"];

                loc@2;

                MyFunction::usage = "MyFunction[arg1] adds 5 to arg1.";

                Begin["`Private`"];

                loc@3;

                abc = 5;
                MyFunction[arg1_] := arg1 + abc;

                End[];

                loc@4;

                EndPackage[];

                loc@5
                ]// Last // Dataset


                enter image description here




                When I load the package <the $ContextPath will have CustomPackage on it, but not CustomPackagePrivate




                Yes, this implements both the public exporting of all CustomPackage functions but without polluting end-users namespaces with implementation details. In code around Location 3, all packages are cleared out thereby eliminating possible conflicts with existing abc definitions in currently loaded packages. This is encapsulation benefitting developers but the encapsulation benefitting end-users, as observed, is that on exiting (at Location 5) $ContextPath contains "CustomPackage`" (to provide access to MyFunction) but not "CustomPackage`Private`" thereby shielding users from symbols used in MyFunction's implementation.



                A programmatic confirmation at Location 5 gives:



                MemberQ["CustomPackage`"]@$ContextPath, 
                MemberQ["CustomPackage`Private`"]@$ContextPath,
                Context["abc"]

                True, False, "Global`"



                At Location 3 in the control-flow, the symbol abc is not contained in any of the contexts defined in $ContextPath, ("CustomPackage`", or "System`") nor is it (yet) in the context defined in $Context ("CustomPackage`Private`"). Consequently, the name abc gets created in the context currently set to $Context. At this location $Context has value "CustomPackage`Private`" and hence the symbol CustomPackage`Private`abc is created. When the control flow then moves on to MyFunction[], "CustomPackage`Private`" is still the value of $Context so this function "sees" abc (hence it not just $ContextPath that determines what a function sees but what is on both$ContextPath and $Context).



                Note how the convention of placing usage definitions at Location 2 is ostensibly for documentation purposes but its more important role is to ensure that the function goes into the package's context (see $Context at Location 2) before subsequently being made available in the implementation and for end-users (see $ContextPath at Locations 3 and 5).



                IMO it is kind of cool how these placement protocols just work intuitively without necessarily keeping front-of-mind all the control-flow manipulations, variable-creation mechanisms etc taking place behind the scenes. Hence this means being very careful changing the framework but also IMHO the time is ripe for such extensions given that the line between users/developers may well be in the process of blurring.






                share|improve this answer











                $endgroup$













                  Your Answer








                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "387"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194963%2fwhat-is-the-accessibility-of-a-packages-private-context-variables%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  10












                  $begingroup$


                  So how does MyFunction know the value of abc at the delayed function call (when it is called) if the Private` context isn't on the $ContextPath




                  There is a misunderstanding here. You are assuming that abc is searched for in some context only when MyFunction[something] is evaluated. This is not the case.



                  $Context and $ContextPath only affect how source code is parsed (not how expressions are evaluated). In other words, they only affect how the text you write in the package file is interpreted and converted into in-memory expressions. Once the package has been loaded with Get, this interpretation has already happened. MyFunction has been interpreted as the symbol CustomPackage`MyFunction and abc has been interpreted as CustomPackage`Private`abc, according to the value of $Context and $ContextPath at the time each was read. These are the full names of these symbols and this is how they exist in memory.



                  Load the package and try this:



                  Block[$ContextPath,
                  Print@Definition[MyFunction]
                  ]


                  You'll see the following printed:



                  CustomPackage`MyFunction[CustomPackage`Private`arg1_] := 
                  CustomPackage`Private`arg1+CustomPackage`Private`abc


                  As you can see, a context is always associated with every symbol.






                  share|improve this answer











                  $endgroup$

















                    10












                    $begingroup$


                    So how does MyFunction know the value of abc at the delayed function call (when it is called) if the Private` context isn't on the $ContextPath




                    There is a misunderstanding here. You are assuming that abc is searched for in some context only when MyFunction[something] is evaluated. This is not the case.



                    $Context and $ContextPath only affect how source code is parsed (not how expressions are evaluated). In other words, they only affect how the text you write in the package file is interpreted and converted into in-memory expressions. Once the package has been loaded with Get, this interpretation has already happened. MyFunction has been interpreted as the symbol CustomPackage`MyFunction and abc has been interpreted as CustomPackage`Private`abc, according to the value of $Context and $ContextPath at the time each was read. These are the full names of these symbols and this is how they exist in memory.



                    Load the package and try this:



                    Block[$ContextPath,
                    Print@Definition[MyFunction]
                    ]


                    You'll see the following printed:



                    CustomPackage`MyFunction[CustomPackage`Private`arg1_] := 
                    CustomPackage`Private`arg1+CustomPackage`Private`abc


                    As you can see, a context is always associated with every symbol.






                    share|improve this answer











                    $endgroup$















                      10












                      10








                      10





                      $begingroup$


                      So how does MyFunction know the value of abc at the delayed function call (when it is called) if the Private` context isn't on the $ContextPath




                      There is a misunderstanding here. You are assuming that abc is searched for in some context only when MyFunction[something] is evaluated. This is not the case.



                      $Context and $ContextPath only affect how source code is parsed (not how expressions are evaluated). In other words, they only affect how the text you write in the package file is interpreted and converted into in-memory expressions. Once the package has been loaded with Get, this interpretation has already happened. MyFunction has been interpreted as the symbol CustomPackage`MyFunction and abc has been interpreted as CustomPackage`Private`abc, according to the value of $Context and $ContextPath at the time each was read. These are the full names of these symbols and this is how they exist in memory.



                      Load the package and try this:



                      Block[$ContextPath,
                      Print@Definition[MyFunction]
                      ]


                      You'll see the following printed:



                      CustomPackage`MyFunction[CustomPackage`Private`arg1_] := 
                      CustomPackage`Private`arg1+CustomPackage`Private`abc


                      As you can see, a context is always associated with every symbol.






                      share|improve this answer











                      $endgroup$




                      So how does MyFunction know the value of abc at the delayed function call (when it is called) if the Private` context isn't on the $ContextPath




                      There is a misunderstanding here. You are assuming that abc is searched for in some context only when MyFunction[something] is evaluated. This is not the case.



                      $Context and $ContextPath only affect how source code is parsed (not how expressions are evaluated). In other words, they only affect how the text you write in the package file is interpreted and converted into in-memory expressions. Once the package has been loaded with Get, this interpretation has already happened. MyFunction has been interpreted as the symbol CustomPackage`MyFunction and abc has been interpreted as CustomPackage`Private`abc, according to the value of $Context and $ContextPath at the time each was read. These are the full names of these symbols and this is how they exist in memory.



                      Load the package and try this:



                      Block[$ContextPath,
                      Print@Definition[MyFunction]
                      ]


                      You'll see the following printed:



                      CustomPackage`MyFunction[CustomPackage`Private`arg1_] := 
                      CustomPackage`Private`arg1+CustomPackage`Private`abc


                      As you can see, a context is always associated with every symbol.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 10 at 20:07

























                      answered Apr 10 at 19:47









                      SzabolcsSzabolcs

                      165k14450954




                      165k14450954





















                          7












                          $begingroup$

                          Begin["`Private`"]; sets the current $Context to "CustomPackage `Private`". This causes two things:



                          • The symbol abc will be searched in the current context first, thus in"CustomPackage`Private`". Only if it is not found there, the search goes on along $ContextPath.


                          • If no matching symbol is found this way, a new symbol abc is created, namely in the current $Context which is "CustomPackage`Private`". So the full symbol name is "CustomPackage`Private`abc".


                          For example, running your code in a fresh kernel and executing



                          ??MyFunction


                          reveals that the full definition of MyFunction is




                          MyFunction[CustomPackage`Private`arg1_]:=CustomPackage`Private`arg1+CustomPackage`Private`abc




                          Moreover, with



                           ?*`abc


                          you see that the only symbol in all contexts that matches abc is CustomPackage`Private`abc and has the value 5 assigned to it.






                          share|improve this answer











                          $endgroup$

















                            7












                            $begingroup$

                            Begin["`Private`"]; sets the current $Context to "CustomPackage `Private`". This causes two things:



                            • The symbol abc will be searched in the current context first, thus in"CustomPackage`Private`". Only if it is not found there, the search goes on along $ContextPath.


                            • If no matching symbol is found this way, a new symbol abc is created, namely in the current $Context which is "CustomPackage`Private`". So the full symbol name is "CustomPackage`Private`abc".


                            For example, running your code in a fresh kernel and executing



                            ??MyFunction


                            reveals that the full definition of MyFunction is




                            MyFunction[CustomPackage`Private`arg1_]:=CustomPackage`Private`arg1+CustomPackage`Private`abc




                            Moreover, with



                             ?*`abc


                            you see that the only symbol in all contexts that matches abc is CustomPackage`Private`abc and has the value 5 assigned to it.






                            share|improve this answer











                            $endgroup$















                              7












                              7








                              7





                              $begingroup$

                              Begin["`Private`"]; sets the current $Context to "CustomPackage `Private`". This causes two things:



                              • The symbol abc will be searched in the current context first, thus in"CustomPackage`Private`". Only if it is not found there, the search goes on along $ContextPath.


                              • If no matching symbol is found this way, a new symbol abc is created, namely in the current $Context which is "CustomPackage`Private`". So the full symbol name is "CustomPackage`Private`abc".


                              For example, running your code in a fresh kernel and executing



                              ??MyFunction


                              reveals that the full definition of MyFunction is




                              MyFunction[CustomPackage`Private`arg1_]:=CustomPackage`Private`arg1+CustomPackage`Private`abc




                              Moreover, with



                               ?*`abc


                              you see that the only symbol in all contexts that matches abc is CustomPackage`Private`abc and has the value 5 assigned to it.






                              share|improve this answer











                              $endgroup$



                              Begin["`Private`"]; sets the current $Context to "CustomPackage `Private`". This causes two things:



                              • The symbol abc will be searched in the current context first, thus in"CustomPackage`Private`". Only if it is not found there, the search goes on along $ContextPath.


                              • If no matching symbol is found this way, a new symbol abc is created, namely in the current $Context which is "CustomPackage`Private`". So the full symbol name is "CustomPackage`Private`abc".


                              For example, running your code in a fresh kernel and executing



                              ??MyFunction


                              reveals that the full definition of MyFunction is




                              MyFunction[CustomPackage`Private`arg1_]:=CustomPackage`Private`arg1+CustomPackage`Private`abc




                              Moreover, with



                               ?*`abc


                              you see that the only symbol in all contexts that matches abc is CustomPackage`Private`abc and has the value 5 assigned to it.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Apr 10 at 19:35

























                              answered Apr 10 at 19:25









                              Henrik SchumacherHenrik Schumacher

                              60.5k584170




                              60.5k584170





















                                  6












                                  $begingroup$

                                  All symbols are created at load time, so when you do:



                                  BeginPackage["X`"];

                                  x::usage="Declaring x as an exported symbol in the X` context";

                                  Begin["`SomePrivateContext`"];

                                  x[a_]:=b

                                  End[];

                                  EndPackage[];


                                  x was created as X`x but the DownValues of x reference X`SomePrivateContext`a and X`SomePrivateContext`b which were created at the time the function was defined. These symbols are unique, so that reference only ever points that a single object.






                                  share|improve this answer









                                  $endgroup$

















                                    6












                                    $begingroup$

                                    All symbols are created at load time, so when you do:



                                    BeginPackage["X`"];

                                    x::usage="Declaring x as an exported symbol in the X` context";

                                    Begin["`SomePrivateContext`"];

                                    x[a_]:=b

                                    End[];

                                    EndPackage[];


                                    x was created as X`x but the DownValues of x reference X`SomePrivateContext`a and X`SomePrivateContext`b which were created at the time the function was defined. These symbols are unique, so that reference only ever points that a single object.






                                    share|improve this answer









                                    $endgroup$















                                      6












                                      6








                                      6





                                      $begingroup$

                                      All symbols are created at load time, so when you do:



                                      BeginPackage["X`"];

                                      x::usage="Declaring x as an exported symbol in the X` context";

                                      Begin["`SomePrivateContext`"];

                                      x[a_]:=b

                                      End[];

                                      EndPackage[];


                                      x was created as X`x but the DownValues of x reference X`SomePrivateContext`a and X`SomePrivateContext`b which were created at the time the function was defined. These symbols are unique, so that reference only ever points that a single object.






                                      share|improve this answer









                                      $endgroup$



                                      All symbols are created at load time, so when you do:



                                      BeginPackage["X`"];

                                      x::usage="Declaring x as an exported symbol in the X` context";

                                      Begin["`SomePrivateContext`"];

                                      x[a_]:=b

                                      End[];

                                      EndPackage[];


                                      x was created as X`x but the DownValues of x reference X`SomePrivateContext`a and X`SomePrivateContext`b which were created at the time the function was defined. These symbols are unique, so that reference only ever points that a single object.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 10 at 19:20









                                      b3m2a1b3m2a1

                                      28.9k360167




                                      28.9k360167





















                                          3












                                          $begingroup$


                                          So how does MyFunction know the value of abc
                                          at the delayed function call (when it is called)
                                          if the Private` context isn't on the $ContextPath?




                                          because "CustomPackage`Private`" is the value of $Context when MyFunction is defined (i.e. it is not just $ContextPath that determines what a function sees but also what is on $Context).




                                          TL:DR



                                          This is a timely question because it indirectly touches upon the competing imperatives of developers and end-users. To the question itself:



                                          The whole point of packages is that they are a form of encapsulation that allows developers to, without interferance, implement functionality for end-users without bothering them with the underlying details. In particular, the encapsulation involves controlling namespaces so that the underlying details can involve symbols that help implement the functionality but ultimately don't end-up polluting a user's namespace. All symbols defined in a "*`Private`" namespace are created for exactly this purpose.



                                          Hence in the OP's example, the variable abc is an underlying detail for the implementation of the public MyFunction. The developer needs the "detail" of abc but this particular symbol is of no direct interest to an end-user who typically just ends up calling MyFunction[].



                                          The package layout achieves this encapsulation by manipulating $ContextPath and $Context as the control-flow passes through the package when it is first loaded. This is described in the other answers and documentation but it can be useful to see it directly:



                                          loc[n_] := Sow[<|
                                          "Location" -> n,
                                          "$Context" -> $Context,
                                          "$ContextPath" -> $ContextPath|>];

                                          Reap[

                                          loc@1;

                                          BeginPackage["CustomPackage`"];

                                          loc@2;

                                          MyFunction::usage = "MyFunction[arg1] adds 5 to arg1.";

                                          Begin["`Private`"];

                                          loc@3;

                                          abc = 5;
                                          MyFunction[arg1_] := arg1 + abc;

                                          End[];

                                          loc@4;

                                          EndPackage[];

                                          loc@5
                                          ]// Last // Dataset


                                          enter image description here




                                          When I load the package <the $ContextPath will have CustomPackage on it, but not CustomPackagePrivate




                                          Yes, this implements both the public exporting of all CustomPackage functions but without polluting end-users namespaces with implementation details. In code around Location 3, all packages are cleared out thereby eliminating possible conflicts with existing abc definitions in currently loaded packages. This is encapsulation benefitting developers but the encapsulation benefitting end-users, as observed, is that on exiting (at Location 5) $ContextPath contains "CustomPackage`" (to provide access to MyFunction) but not "CustomPackage`Private`" thereby shielding users from symbols used in MyFunction's implementation.



                                          A programmatic confirmation at Location 5 gives:



                                          MemberQ["CustomPackage`"]@$ContextPath, 
                                          MemberQ["CustomPackage`Private`"]@$ContextPath,
                                          Context["abc"]

                                          True, False, "Global`"



                                          At Location 3 in the control-flow, the symbol abc is not contained in any of the contexts defined in $ContextPath, ("CustomPackage`", or "System`") nor is it (yet) in the context defined in $Context ("CustomPackage`Private`"). Consequently, the name abc gets created in the context currently set to $Context. At this location $Context has value "CustomPackage`Private`" and hence the symbol CustomPackage`Private`abc is created. When the control flow then moves on to MyFunction[], "CustomPackage`Private`" is still the value of $Context so this function "sees" abc (hence it not just $ContextPath that determines what a function sees but what is on both$ContextPath and $Context).



                                          Note how the convention of placing usage definitions at Location 2 is ostensibly for documentation purposes but its more important role is to ensure that the function goes into the package's context (see $Context at Location 2) before subsequently being made available in the implementation and for end-users (see $ContextPath at Locations 3 and 5).



                                          IMO it is kind of cool how these placement protocols just work intuitively without necessarily keeping front-of-mind all the control-flow manipulations, variable-creation mechanisms etc taking place behind the scenes. Hence this means being very careful changing the framework but also IMHO the time is ripe for such extensions given that the line between users/developers may well be in the process of blurring.






                                          share|improve this answer











                                          $endgroup$

















                                            3












                                            $begingroup$


                                            So how does MyFunction know the value of abc
                                            at the delayed function call (when it is called)
                                            if the Private` context isn't on the $ContextPath?




                                            because "CustomPackage`Private`" is the value of $Context when MyFunction is defined (i.e. it is not just $ContextPath that determines what a function sees but also what is on $Context).




                                            TL:DR



                                            This is a timely question because it indirectly touches upon the competing imperatives of developers and end-users. To the question itself:



                                            The whole point of packages is that they are a form of encapsulation that allows developers to, without interferance, implement functionality for end-users without bothering them with the underlying details. In particular, the encapsulation involves controlling namespaces so that the underlying details can involve symbols that help implement the functionality but ultimately don't end-up polluting a user's namespace. All symbols defined in a "*`Private`" namespace are created for exactly this purpose.



                                            Hence in the OP's example, the variable abc is an underlying detail for the implementation of the public MyFunction. The developer needs the "detail" of abc but this particular symbol is of no direct interest to an end-user who typically just ends up calling MyFunction[].



                                            The package layout achieves this encapsulation by manipulating $ContextPath and $Context as the control-flow passes through the package when it is first loaded. This is described in the other answers and documentation but it can be useful to see it directly:



                                            loc[n_] := Sow[<|
                                            "Location" -> n,
                                            "$Context" -> $Context,
                                            "$ContextPath" -> $ContextPath|>];

                                            Reap[

                                            loc@1;

                                            BeginPackage["CustomPackage`"];

                                            loc@2;

                                            MyFunction::usage = "MyFunction[arg1] adds 5 to arg1.";

                                            Begin["`Private`"];

                                            loc@3;

                                            abc = 5;
                                            MyFunction[arg1_] := arg1 + abc;

                                            End[];

                                            loc@4;

                                            EndPackage[];

                                            loc@5
                                            ]// Last // Dataset


                                            enter image description here




                                            When I load the package <the $ContextPath will have CustomPackage on it, but not CustomPackagePrivate




                                            Yes, this implements both the public exporting of all CustomPackage functions but without polluting end-users namespaces with implementation details. In code around Location 3, all packages are cleared out thereby eliminating possible conflicts with existing abc definitions in currently loaded packages. This is encapsulation benefitting developers but the encapsulation benefitting end-users, as observed, is that on exiting (at Location 5) $ContextPath contains "CustomPackage`" (to provide access to MyFunction) but not "CustomPackage`Private`" thereby shielding users from symbols used in MyFunction's implementation.



                                            A programmatic confirmation at Location 5 gives:



                                            MemberQ["CustomPackage`"]@$ContextPath, 
                                            MemberQ["CustomPackage`Private`"]@$ContextPath,
                                            Context["abc"]

                                            True, False, "Global`"



                                            At Location 3 in the control-flow, the symbol abc is not contained in any of the contexts defined in $ContextPath, ("CustomPackage`", or "System`") nor is it (yet) in the context defined in $Context ("CustomPackage`Private`"). Consequently, the name abc gets created in the context currently set to $Context. At this location $Context has value "CustomPackage`Private`" and hence the symbol CustomPackage`Private`abc is created. When the control flow then moves on to MyFunction[], "CustomPackage`Private`" is still the value of $Context so this function "sees" abc (hence it not just $ContextPath that determines what a function sees but what is on both$ContextPath and $Context).



                                            Note how the convention of placing usage definitions at Location 2 is ostensibly for documentation purposes but its more important role is to ensure that the function goes into the package's context (see $Context at Location 2) before subsequently being made available in the implementation and for end-users (see $ContextPath at Locations 3 and 5).



                                            IMO it is kind of cool how these placement protocols just work intuitively without necessarily keeping front-of-mind all the control-flow manipulations, variable-creation mechanisms etc taking place behind the scenes. Hence this means being very careful changing the framework but also IMHO the time is ripe for such extensions given that the line between users/developers may well be in the process of blurring.






                                            share|improve this answer











                                            $endgroup$















                                              3












                                              3








                                              3





                                              $begingroup$


                                              So how does MyFunction know the value of abc
                                              at the delayed function call (when it is called)
                                              if the Private` context isn't on the $ContextPath?




                                              because "CustomPackage`Private`" is the value of $Context when MyFunction is defined (i.e. it is not just $ContextPath that determines what a function sees but also what is on $Context).




                                              TL:DR



                                              This is a timely question because it indirectly touches upon the competing imperatives of developers and end-users. To the question itself:



                                              The whole point of packages is that they are a form of encapsulation that allows developers to, without interferance, implement functionality for end-users without bothering them with the underlying details. In particular, the encapsulation involves controlling namespaces so that the underlying details can involve symbols that help implement the functionality but ultimately don't end-up polluting a user's namespace. All symbols defined in a "*`Private`" namespace are created for exactly this purpose.



                                              Hence in the OP's example, the variable abc is an underlying detail for the implementation of the public MyFunction. The developer needs the "detail" of abc but this particular symbol is of no direct interest to an end-user who typically just ends up calling MyFunction[].



                                              The package layout achieves this encapsulation by manipulating $ContextPath and $Context as the control-flow passes through the package when it is first loaded. This is described in the other answers and documentation but it can be useful to see it directly:



                                              loc[n_] := Sow[<|
                                              "Location" -> n,
                                              "$Context" -> $Context,
                                              "$ContextPath" -> $ContextPath|>];

                                              Reap[

                                              loc@1;

                                              BeginPackage["CustomPackage`"];

                                              loc@2;

                                              MyFunction::usage = "MyFunction[arg1] adds 5 to arg1.";

                                              Begin["`Private`"];

                                              loc@3;

                                              abc = 5;
                                              MyFunction[arg1_] := arg1 + abc;

                                              End[];

                                              loc@4;

                                              EndPackage[];

                                              loc@5
                                              ]// Last // Dataset


                                              enter image description here




                                              When I load the package <the $ContextPath will have CustomPackage on it, but not CustomPackagePrivate




                                              Yes, this implements both the public exporting of all CustomPackage functions but without polluting end-users namespaces with implementation details. In code around Location 3, all packages are cleared out thereby eliminating possible conflicts with existing abc definitions in currently loaded packages. This is encapsulation benefitting developers but the encapsulation benefitting end-users, as observed, is that on exiting (at Location 5) $ContextPath contains "CustomPackage`" (to provide access to MyFunction) but not "CustomPackage`Private`" thereby shielding users from symbols used in MyFunction's implementation.



                                              A programmatic confirmation at Location 5 gives:



                                              MemberQ["CustomPackage`"]@$ContextPath, 
                                              MemberQ["CustomPackage`Private`"]@$ContextPath,
                                              Context["abc"]

                                              True, False, "Global`"



                                              At Location 3 in the control-flow, the symbol abc is not contained in any of the contexts defined in $ContextPath, ("CustomPackage`", or "System`") nor is it (yet) in the context defined in $Context ("CustomPackage`Private`"). Consequently, the name abc gets created in the context currently set to $Context. At this location $Context has value "CustomPackage`Private`" and hence the symbol CustomPackage`Private`abc is created. When the control flow then moves on to MyFunction[], "CustomPackage`Private`" is still the value of $Context so this function "sees" abc (hence it not just $ContextPath that determines what a function sees but what is on both$ContextPath and $Context).



                                              Note how the convention of placing usage definitions at Location 2 is ostensibly for documentation purposes but its more important role is to ensure that the function goes into the package's context (see $Context at Location 2) before subsequently being made available in the implementation and for end-users (see $ContextPath at Locations 3 and 5).



                                              IMO it is kind of cool how these placement protocols just work intuitively without necessarily keeping front-of-mind all the control-flow manipulations, variable-creation mechanisms etc taking place behind the scenes. Hence this means being very careful changing the framework but also IMHO the time is ripe for such extensions given that the line between users/developers may well be in the process of blurring.






                                              share|improve this answer











                                              $endgroup$




                                              So how does MyFunction know the value of abc
                                              at the delayed function call (when it is called)
                                              if the Private` context isn't on the $ContextPath?




                                              because "CustomPackage`Private`" is the value of $Context when MyFunction is defined (i.e. it is not just $ContextPath that determines what a function sees but also what is on $Context).




                                              TL:DR



                                              This is a timely question because it indirectly touches upon the competing imperatives of developers and end-users. To the question itself:



                                              The whole point of packages is that they are a form of encapsulation that allows developers to, without interferance, implement functionality for end-users without bothering them with the underlying details. In particular, the encapsulation involves controlling namespaces so that the underlying details can involve symbols that help implement the functionality but ultimately don't end-up polluting a user's namespace. All symbols defined in a "*`Private`" namespace are created for exactly this purpose.



                                              Hence in the OP's example, the variable abc is an underlying detail for the implementation of the public MyFunction. The developer needs the "detail" of abc but this particular symbol is of no direct interest to an end-user who typically just ends up calling MyFunction[].



                                              The package layout achieves this encapsulation by manipulating $ContextPath and $Context as the control-flow passes through the package when it is first loaded. This is described in the other answers and documentation but it can be useful to see it directly:



                                              loc[n_] := Sow[<|
                                              "Location" -> n,
                                              "$Context" -> $Context,
                                              "$ContextPath" -> $ContextPath|>];

                                              Reap[

                                              loc@1;

                                              BeginPackage["CustomPackage`"];

                                              loc@2;

                                              MyFunction::usage = "MyFunction[arg1] adds 5 to arg1.";

                                              Begin["`Private`"];

                                              loc@3;

                                              abc = 5;
                                              MyFunction[arg1_] := arg1 + abc;

                                              End[];

                                              loc@4;

                                              EndPackage[];

                                              loc@5
                                              ]// Last // Dataset


                                              enter image description here




                                              When I load the package <the $ContextPath will have CustomPackage on it, but not CustomPackagePrivate




                                              Yes, this implements both the public exporting of all CustomPackage functions but without polluting end-users namespaces with implementation details. In code around Location 3, all packages are cleared out thereby eliminating possible conflicts with existing abc definitions in currently loaded packages. This is encapsulation benefitting developers but the encapsulation benefitting end-users, as observed, is that on exiting (at Location 5) $ContextPath contains "CustomPackage`" (to provide access to MyFunction) but not "CustomPackage`Private`" thereby shielding users from symbols used in MyFunction's implementation.



                                              A programmatic confirmation at Location 5 gives:



                                              MemberQ["CustomPackage`"]@$ContextPath, 
                                              MemberQ["CustomPackage`Private`"]@$ContextPath,
                                              Context["abc"]

                                              True, False, "Global`"



                                              At Location 3 in the control-flow, the symbol abc is not contained in any of the contexts defined in $ContextPath, ("CustomPackage`", or "System`") nor is it (yet) in the context defined in $Context ("CustomPackage`Private`"). Consequently, the name abc gets created in the context currently set to $Context. At this location $Context has value "CustomPackage`Private`" and hence the symbol CustomPackage`Private`abc is created. When the control flow then moves on to MyFunction[], "CustomPackage`Private`" is still the value of $Context so this function "sees" abc (hence it not just $ContextPath that determines what a function sees but what is on both$ContextPath and $Context).



                                              Note how the convention of placing usage definitions at Location 2 is ostensibly for documentation purposes but its more important role is to ensure that the function goes into the package's context (see $Context at Location 2) before subsequently being made available in the implementation and for end-users (see $ContextPath at Locations 3 and 5).



                                              IMO it is kind of cool how these placement protocols just work intuitively without necessarily keeping front-of-mind all the control-flow manipulations, variable-creation mechanisms etc taking place behind the scenes. Hence this means being very careful changing the framework but also IMHO the time is ripe for such extensions given that the line between users/developers may well be in the process of blurring.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Apr 14 at 0:37

























                                              answered Apr 11 at 4:18









                                              Ronald MonsonRonald Monson

                                              3,1831634




                                              3,1831634



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Mathematica Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  Use MathJax to format equations. MathJax reference.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194963%2fwhat-is-the-accessibility-of-a-packages-private-context-variables%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

                                                  Crop image to path created in TikZ? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Crop an inserted image?TikZ pictures does not appear in posterImage behind and beyond crop marks?Tikz picture as large as possible on A4 PageTransparency vs image compression dilemmaHow to crop background from image automatically?Image does not cropTikzexternal capturing crop marks when externalizing pgfplots?How to include image path that contains a dollar signCrop image with left size given

                                                  Romeo and Juliet ContentsCharactersSynopsisSourcesDate and textThemes and motifsCriticism and interpretationLegacyScene by sceneSee alsoNotes and referencesSourcesExternal linksNavigation menu"Consumer Price Index (estimate) 1800–"10.2307/28710160037-3222287101610.1093/res/II.5.31910.2307/45967845967810.2307/2869925286992510.1525/jams.1982.35.3.03a00050"Dada Masilo: South African dancer who breaks the rules"10.1093/res/os-XV.57.1610.2307/28680942868094"Sweet Sorrow: Mann-Korman's Romeo and Juliet Closes Sept. 5 at MN's Ordway"the original10.2307/45957745957710.1017/CCOL0521570476.009"Ram Leela box office collections hit massive Rs 100 crore, pulverises prediction"Archived"Broadway Revival of Romeo and Juliet, Starring Orlando Bloom and Condola Rashad, Will Close Dec. 8"Archived10.1075/jhp.7.1.04hon"Wherefore art thou, Romeo? To make us laugh at Navy Pier"the original10.1093/gmo/9781561592630.article.O006772"Ram-leela Review Roundup: Critics Hail Film as Best Adaptation of Romeo and Juliet"Archived10.2307/31946310047-77293194631"Romeo and Juliet get Twitter treatment""Juliet's Nurse by Lois Leveen""Romeo and Juliet: Orlando Bloom's Broadway Debut Released in Theaters for Valentine's Day"Archived"Romeo and Juliet Has No Balcony"10.1093/gmo/9781561592630.article.O00778110.2307/2867423286742310.1076/enst.82.2.115.959510.1080/00138380601042675"A plague o' both your houses: error in GCSE exam paper forces apology""Juliet of the Five O'Clock Shadow, and Other Wonders"10.2307/33912430027-4321339124310.2307/28487440038-7134284874410.2307/29123140149-661129123144728341M"Weekender Guide: Shakespeare on The Drive""balcony"UK public library membership"romeo"UK public library membership10.1017/CCOL9780521844291"Post-Zionist Critique on Israel and the Palestinians Part III: Popular Culture"10.2307/25379071533-86140377-919X2537907"Capulets and Montagues: UK exam board admit mixing names up in Romeo and Juliet paper"Istoria Novellamente Ritrovata di Due Nobili Amanti2027/mdp.390150822329610820-750X"GCSE exam error: Board accidentally rewrites Shakespeare"10.2307/29176390149-66112917639"Exam board apologises after error in English GCSE paper which confused characters in Shakespeare's Romeo and Juliet""From Mariotto and Ganozza to Romeo and Guilietta: Metamorphoses of a Renaissance Tale"10.2307/37323537323510.2307/2867455286745510.2307/28678912867891"10 Questions for Taylor Swift"10.2307/28680922868092"Haymarket Theatre""The Zeffirelli Way: Revealing Talk by Florentine Director""Michael Smuin: 1938-2007 / Prolific dance director had showy career"The Life and Art of Edwin BoothRomeo and JulietRomeo and JulietRomeo and JulietRomeo and JulietEasy Read Romeo and JulietRomeo and Julieteeecb12003684p(data)4099369-3n8211610759dbe00d-a9e2-41a3-b2c1-977dd692899302814385X313670221313670221