Protect

Protect[s1,s2,]

sets the attribute Protected for the symbols si.

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 builtin 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 all

Basic Examples  (1)

Define and protect a function:

The function definition can no longer be changed:

Scope  (7)

Symbol Inputs  (2)

Protect several symbols:

Use a combination of symbols and symbol names:

Only x3 remains unprotected:

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)

Unprotect a system symbol to make a definition for it:

Restore protection:

Protect all symbols in a package but still allow definitions to be set when the package is reloaded:

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:

Protect has the attribute HoldAll:

This protects symbol itself:

This protects its value:

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:

Unprotect the system symbols that were wrongly protected:

Wolfram Research (1988), Protect, Wolfram Language function, https://reference.wolfram.com/language/ref/Protect.html (updated 2022).

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_2024_protect, author="Wolfram Research", title="{Protect}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/Protect.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_protect, organization={Wolfram Research}, title={Protect}, year={2022}, url={https://reference.wolfram.com/language/ref/Protect.html}, note=[Accessed: 22-December-2024 ]}