Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
BlockProtected
Modify definitions of protected symbols and ensure that their attributes are restored
ResourceFunction["BlockProtected"][{sym1,sym2,…},expr] evaluates expr with symbols symi unprotected and restores their protected attribute before returning the result. | |
ResourceFunction["BlockProtected"][{…,symi=vali,…},expr] sets initial values for the symi. | |
ResourceFunction["BlockProtected"][{syms}] represents an operator form of ResourceFunction["BlockProtected"] that can be applied to an expression. |
Details
Examples
Basic Examples (2)
Modify the definition of a protected symbol:
| In[1]:= |
| In[2]:= |
| Out[2]= |
The value has changed:
| In[3]:= |
| Out[3]= |
The Protected attribute is restored:
| In[4]:= |
| Out[4]= |
Modify the definition of a protected function:
| In[5]:= |
| In[6]:= |
| Out[6]= |
The definition has changed:
| In[7]:= |
| Out[7]= | ![]() |
Scope (3)
Modify multiple protected symbols:
| In[8]:= |
| In[9]:= |
| Out[9]= |
The definitions have changed:
| In[10]:= |
| Out[10]= | ![]() |
| In[11]:= |
| Out[11]= | ![]() |
Use an initial value:
| In[12]:= |
| Out[12]= |
| In[13]:= |
| Out[13]= | ![]() |
Get the operator form of BlockProtected:
| In[14]:= | ![]() |
| Out[14]= |
Use the operator form:
| In[15]:= |
| Out[15]= |
The value of count was changed:
| In[16]:= |
| Out[16]= | ![]() |
| In[17]:= |
| Out[17]= |
Create another operator form that initializes a value:
| In[18]:= |
| Out[18]= |
| In[19]:= |
| Out[19]= |
Now the value is always reset before evaluating:
| In[20]:= |
| Out[20]= |
Applications (2)
A protected symbol cannot usually be cleared:
| In[21]:= |
| In[22]:= |
Clear protected symbols with BlockProtected:
| In[23]:= |
The value is removed:
| In[24]:= |
| Out[24]= |
Write a protected function that caches results in its definition:
| In[25]:= | ![]() |
| In[26]:= |
The results are cached for faster subsequent evaluations:
| In[27]:= |
| Out[27]= |
| In[28]:= |
| Out[28]= |
The cached value is stored in the definition:
| In[29]:= |
| Out[29]= | ![]() |
Having the symbol be protected avoids accidental modifications:
| In[30]:= |
| In[31]:= |
| Out[31]= | ![]() |
Properties and Relations (4)
Only the Protected attribute is modified within BlockProtected:
| In[32]:= |
| In[33]:= |
| Out[33]= |
The attribute is restored when exiting BlockProtected:
| In[34]:= |
| Out[34]= |
If a symbol is protected when entering BlockProtected, it will be protected when exiting, even if all other definitions are cleared:
| In[35]:= |
| Out[35]= |
| In[36]:= |
| Out[36]= |
If a symbol is unprotected going into BlockProtected, it will be unprotected when coming out:
| In[37]:= |
| In[38]:= |
| Out[38]= |
| In[39]:= |
| Out[39]= |
BlockProtected will ensure that attributes for symbols are restored even if evaluation is interrupted:
| In[40]:= | ![]() |
| In[41]:= |
| Out[41]= |
| In[42]:= |
| Out[42]= | ![]() |
| In[43]:= | ![]() |
| Out[43]= |
| In[44]:= |
| Out[44]= | ![]() |
BlockProtected is functionally similar to using Unprotect and Protect around the evaluation:
| In[45]:= | ![]() |
| In[46]:= |
| Out[46]= | ![]() |
However, this does not prevent evaluation interruptions from leaving symbols in an unprotected state:
| In[47]:= |
| Out[47]= |
| In[48]:= |
| Out[48]= | ![]() |
Possible Issues (3)
Symbols that are locked and protected cannot be localized with BlockProtected:
| In[49]:= | ![]() |
| Out[49]= | ![]() |
The definition is unchanged:
| In[50]:= |
| Out[50]= | ![]() |
BlockProtected only ensures that symbols are unprotected before evaluating; it does not prevent symbols from being protected during evaluation:
| In[51]:= | ![]() |
| Out[51]= |
The second increment of counter failed since it became protected:
| In[52]:= |
| Out[52]= |
Local variables must be symbols:
| In[53]:= |
| Out[53]= | ![]() |
| In[54]:= |
| Out[54]= |
Related Links
- Tutorial: Blocks and Local Values
- Tutorial: Blocks Compared with Modules
- Guide: Package Bulletproofing
- Guide: Package Development
- Guide: Cookie Management
- Guide: Scoping Constructs
- Guide: Language Overview
- Guide: Procedural Programming
- Fast Introduction for Programmers: Procedures
- An Elementary Introduction to the Wolfram Language
- NKS|Online
Requirements
Wolfram Language 11.3 (March 2018) or above
Version History
- 2.0.0 – 09 February 2021
- 1.0.0 – 15 August 2018
Related Resources
Related Symbols
License Information
This work is licensed under a Creative Commons Attribution 4.0 International License




![count = 0;
Protect[count];
withCounter = ResourceFunction["BlockProtected"][{count}]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/d9a/d9a4ea11-83a7-4a49-8e1e-35bfe8a31648/437a7ee054b72471.png)

![CachedResourceFunction[id_] := ResourceFunction["BlockProtected"][{CachedResourceFunction},
CachedResourceFunction[id] = ResourceFunction[id, "Function"]
];](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/d9a/d9a4ea11-83a7-4a49-8e1e-35bfe8a31648/128b81823587ce35.png)


![Unprotect[f]; ClearAll[f];
f[x_] := 1/x;
Protect[f];](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/d9a/d9a4ea11-83a7-4a49-8e1e-35bfe8a31648/1175d33f9d96b16b.png)

![Catch[
ResourceFunction["BlockProtected"][{f}, Throw[f[0] = 0]]
]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/d9a/d9a4ea11-83a7-4a49-8e1e-35bfe8a31648/665569fe8fe0425f.png)

![modifyProtected // ClearAll;
modifyProtected // Attributes = {HoldAll};
modifyProtected[f_, eval_] :=
Module[{result},
Unprotect[f];
result = eval;
Protect[f];
result
];](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/d9a/d9a4ea11-83a7-4a49-8e1e-35bfe8a31648/5abd77ff636382a7.png)


![locked = 1;
locked // Attributes = {Locked, Protected};
ResourceFunction["BlockProtected"][{locked}, locked = 2]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/d9a/d9a4ea11-83a7-4a49-8e1e-35bfe8a31648/3ca7af1a4bf3237c.png)


![counter = 1;
Protect[counter];
ResourceFunction["BlockProtected"][{counter}, counter++; Protect[counter]; counter++]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/d9a/d9a4ea11-83a7-4a49-8e1e-35bfe8a31648/446ff77b9db62dd7.png)
