Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a way to set what symbols's definitions I do not want to store?

Consider the following code:

f := $MachineID;
SetDirectory[NotebookDirectory[]];
Save["testm.m", f];

ClearAll@f

<< "testm.m"
f
Set::specset: Cannot change the value of special symbol $MachineID. >>
0000-00000-00001 (*id of storing machine*)
0000-00000-00002 (*id of getting machine*)

This happens bacause testm.m contains:

f := $MachineID
$MachineID = "0000-00000-00001"

Questions:

  • Is there nicer way to avoid this than my approach:

    f:= Symbol["MachineID"] 
    

    ?

  • Is it Protected attribute which decides what is going to be stored? Because there is no problem if f:=SystemID but:

    Attributes[{$SystemID, $MachineID}]
    
    {{Locked, Protected}, {}}
    

Edit:

  • Why MachineID is not protected? - The bounty is founded for this question.
share|improve this question
    
Nice question! I agree that it seems odd that $MachineID is not Protected –  Jacob Akkerboom Aug 27 '13 at 11:42
    
@JacobAkkerboom Hello,:) yes, in fact I wouldn't have known this but there is something else what causes the error msg and without this my MachineID could have been distributed :D. –  Kuba Aug 27 '13 at 11:45
    
ah yes, I guess that could be a privacy issue. –  Jacob Akkerboom Aug 27 '13 at 12:03
2  
I am unhappy that you have asked two very different questions here causing two separate answer threads to come into existence for this one question. It makes a confusing mess for anyone trying to figure out what this post is really about. You really should have posted two questions. –  m_goldberg Aug 27 '13 at 21:46
1  
Related: Why the Block command does not forget the $ContextPath variable. Perhaps we should investigate the extent of this relation, rm-rfs list is similar to mine, but not the same. –  Jacob Akkerboom Mar 12 at 11:21
show 6 more comments

2 Answers 2

up vote 6 down vote accepted

From the documentation:

Save: Save uses FullDefinition to include subsidiary definitions.

FullDefinition: FullDefinition[symbol] recursively prints as all definitions for the symbol, and for the symbols that appear in these definitions, unless those symbols have the attribute Protected.

So if you want to not save $MachineID you could do:

Protect[$MachineID];
Save["testm.m", f];
Unprotect[$MachineID];
share|improve this answer
    
Thank you, it seems I have to dig more in documentation next time :) –  Kuba Aug 27 '13 at 11:05
3  
Don't you think it is strange that $MachineID is not protected? –  Kuba Aug 27 '13 at 11:05
add comment

Extended comment

I found the following interesting. I guess this happens for more Symbols than just $MachineID. The following tries to assign to special symbols starting with a $ and gives an overview of whether that was allowed and what the attributes are. Warning: you may want to restart the kernel after this.

list =
  Function[
      xxxx, {HoldForm[xxxx], Check[xxxx = xxxx; "succes", "failure"], 
       Sequence @@ Attributes[xxxx]}, HoldAllComplete] @@ 
     ToExpression[#, InputForm, HoldComplete] & /@ 
   DeleteCases[Names["$*"], "$PrePrint" | "$Pre" | "$Post"];
list // TableForm
(*gives a big table*)

The following then gives a list of symbols similar to $MachineID in this respect

Select[list, 
  Function[Not[FreeQ[#, "failure"]] && FreeQ[#, Protected]]] // Column


(*output*)

    {$ActivationGroupID,failure}
    {$ActivationKey,failure}
    {$ActivationUserRegistered,failure}
    {$InputFileName,failure}
    {$LicenseExpirationDate,failure}
    {$LicenseID,failure}
    {$LicenseProcesses,failure}
    {$LicenseServer,failure}
    {$LicenseSubprocesses,failure}
    {$LicenseType,failure}
    {$MachineAddresses,failure}
    {$MachineDomain,failure}
    {$MachineDomains,failure}
    {$MachineID,failure}
    {$MachineName,failure}
    {$MaxLicenseProcesses,failure}
    {$MaxLicenseSubprocesses,failure}
    {$NetworkLicense,failure}
    {$ParentProcessID,failure}
    {$PatchLevelID,failure}
    {$ProcessID,failure}
    {$UserName,failure}
share|improve this answer
    
Thanks, I'm curious why it is so. Btw. what causes the error masg if the symbol is not protected? –  Kuba Aug 27 '13 at 12:28
    
@Kuba I don't know, the documentation has no special entry on this message. I suppose that that is a specific message just for special symbols. That is, the ones above, and probably similar symbols that do have Protected as well. –  Jacob Akkerboom Aug 27 '13 at 12:33
    
this answer by rm-rf is related. –  Jacob Akkerboom Mar 12 at 11:22
add comment

Your Answer

 
discard

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.