Protect
Details
- A typical sequence in adding your own rules for built‐in functions is Unprotect[f];definition;Protect[f]. »
- The pattern patt can be given as a string with metacharacters, as StringExpression[…] or as RegularExpression["regex"]. »
- Protect allows abbreviated string patterns containing the following metacharacters:
-
* zero or more characters @ one or more characters, excluding uppercase letters - Protect["context`*"] protects all symbols in a particular context. »
- Protect["`*"] protects all symbols in the current context. »
- Protect does not affect symbols with the attribute Locked. »
- Protect has attribute HoldAll. »
Examples
open all close allBasic Examples (1)
Scope (7)
Symbol Inputs (2)
Attributes[{f, g, h}]Protect[f, g, h]Attributes[{f, g, h}]Use a combination of symbols and symbol names:
x1 = 1;x2 = 3;x3 = 5;Protect[{x1, "x2"}]Attributes[{x1, x2, x3}]Using Patterns (5)
Specify symbols to protect as string patterns:
x1 = 1;x2 = 3;y = 5;Protect["x*"]Attributes[{x1, x2, y}]Protect all symbols in the current context:
x1 = 1;x2 = 3;x3 = 5;Protect["`*"]Attributes[{x1, x2, x3}]Protect all symbols in a given context:
abc`x1 = 1;abc`x2 = 3;abc`y = 5;Protect["abc`*"]Attributes[{abc`x1, abc`x2, abc`y}]Protect all 2-character symbols in the current context using StringExpression:
x1 = 1;x2 = 3;y = 5;Protect["`" ~~ _ ~~ _]The symbols x1 and x2 were protected, but y remains unaffected:
Attributes[{x1, x2, y}]Protect all 3-character symbols in the current context using RegularExpression:
x = 1;yy = 3;zzz = 5;Protect[RegularExpression["`..."]]Attributes[{x, yy, zzz}]Applications (2)
Unprotect a system symbol to make a definition for it:
protected = Unprotect[Sqrt]Sqrt[x_ ^ 2] := xSqrt[x ^ 2]Protect[Evaluate[protected]]Protect all symbols in a package but still allow definitions to be set when the package is reloaded:
Begin["`Private`"];Unprotect["`*"];
ClearAll["`*"];f[x_] := x ^ 2;
g[x_] := x ^ 3;Protect["`*"];End[];Properties & Relations (7)
Protect[pattern] protects the same symbols as Protect/@Names[pattern]:
x = 1;y = 2; xy = 3;
Protect["x*"];
Attributes[{x, y, xy}]Unprotect["x*"];
x = 1;y = 2; xy = 3;
Protect /@ Names["x*"];
Attributes[{x, y, xy}]Protecting a symbol is equivalent to setting the Protected attribute:
Protect[f];
SetAttributes[g, Protected];Attributes[{f, g}]Unprotecting a symbol is equivalent to clearing the Protected attribute:
Unprotect[f];
ClearAttributes[g, Protected];Attributes[{f, g}]Protecting a symbol does not affect other attributes:
SetAttributes[f, HoldFirst];Protect[f]Attributes[f]Protect returns the list of symbols actually protected:
Protect[g]If a symbol is already protected, it is not protected again:
Protect[f, g]No more definitions can be made for a protected symbol:
f[x_] := x ^ 3
Protect[f];f[x_] := x ^ 2To modify definitions of a protected symbol, unprotect it first:
Unprotect[f];f[x_] := x ^ 2Protect has the attribute HoldAll:
Definition[Protect]symbol = {f, g, h};Protect[symbol]Attributes[{symbol, f, g, h}]Unprotect[symbol];Protect[Evaluate[symbol]]Attributes[{symbol, f, g, h}]Protect accepts any number of arguments, including zero:
Protect[]Possible Issues (2)
Protect does not affect locked symbols:
SetAttributes[f, Locked]Protect[f]Definition[f]When using a pattern without a context mark, all matching symbols on $ContextPath are protected:
{Cx, Cy, Cxy, Dyy};
protected = Protect["C" ~~ _ ~~ _]The symbol Cxy was unprotected, along with several system symbols:
Attributes[Cxy]Use a pattern with an explicit context mark to avoid potentially matching symbols from system or other contexts:
Protect["`D" ~~ _ ~~ _]Unprotect the system symbols that were wrongly protected:
Unprotect[Evaluate@Most[protected]]See Also
Tech Notes
Related Guides
History
Introduced in 1988 (1.0) | Updated in 2022 (13.2)
Text
Wolfram Research (1988), Protect, Wolfram Language function, https://reference.wolfram.com/language/ref/Protect.html (updated 2022).
CMS
Wolfram Language. 1988. "Protect." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/Protect.html.
APA
Wolfram Language. (1988). Protect. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Protect.html
BibTeX
@misc{reference.wolfram_2026_protect, author="Wolfram Research", title="{Protect}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/Protect.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_protect, organization={Wolfram Research}, title={Protect}, year={2022}, url={https://reference.wolfram.com/language/ref/Protect.html}, note=[Accessed: 13-June-2026]}