Protect
Protect[patt1,patt2,…]
protects all symbols whose names textually match any of the arbitrary string patterns patti.
Protect[{spec1,spec2,…}]
protects any symbols that are equal to or whose names match any of the speci.
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 allclose allScope (7)
Symbol Inputs (2)
Using Patterns (5)
Specify symbols to protect as string patterns:
Protect all symbols in the current context:
Protect all symbols in a given context:
Protect all 2-character symbols in the current context using StringExpression:
The symbols x1 and x2 were protected, but y remains unaffected:
Protect all 3-character symbols in the current context using RegularExpression:
Applications (2)
Properties & Relations (6)
Protect[pattern] protects the same symbols as Protect/@Names[pattern]:
Protecting a symbol is equivalent to setting the Protected attribute:
Unprotecting a symbol is equivalent to clearing the Protected attribute:
Protecting a symbol does not affect other attributes:
Protect returns the list of symbols actually protected:
If a symbol is already protected, it is not protected again:
No more definitions can be made for a protected symbol:
To modify definitions of a protected symbol, unprotect it first:
Possible Issues (2)
Protect does not affect locked symbols:
When using a pattern without a context mark, all matching symbols on $ContextPath are protected:
The symbol Cxy was unprotected, along with several system symbols:
Use a pattern with an explicit context mark to avoid potentially matching symbols from system or other contexts:
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