10
$\begingroup$

When using Unprotect, we can overwrite a functions definition:

Unprotect[Sqrt]
Sqrt[x_]="blahblah"
Protect[Sqrt]

Once this is done, can we revert to the built-in version of Sqrt?

$\endgroup$

2 Answers 2

12
$\begingroup$
Unprotect[Sqrt];
Sqrt[x_] = "blahblah";
Protect[Sqrt];

Sqrt[2]

blahblah

Unprotect[Sqrt];
ClearAll[Sqrt];
Protect[Sqrt];

Sqrt[2]

enter image description here

$\endgroup$
3
$\begingroup$

If you want to keep the previous definition and use built-in function in further operation, you can use ParallelEvaluate

    Unprotect[Sqrt];
    Sqrt[x_] = "blahblah";
    Protect[Sqrt];
    Sqrt[4]
    (*"blahblah"*)
    First@ParallelEvaluate[Sqrt[4]]
    (*2*)
$\endgroup$
1
  • $\begingroup$ That works for a completely different reason, which is that your current context isn't distributed to the parallel kernels. Considering that a function may be defined for use in Parallelize[] or ParallelMap[] or ParallelEvaluate[] and contexts would be distributed, your answer would fail. And if there was no intent for parallelization, your answer is excessive. Why launch kernel for something that can simply be cleared off and return to the value set by a context defined prior to yours? $\endgroup$ Commented Apr 14, 2017 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.