Home >Backend Development >Python Tutorial >Overwrote a Built-in Function? How Do I Get It Back?

Overwrote a Built-in Function? How Do I Get It Back?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-02 04:46:02233browse

Overwrote a Built-in Function? How Do I Get It Back?

Accidentally Overwriting Built-In Functions: A Restoration Guide

Overwriting built-in functions by accident can be frustrating, especially when you're working with a long-running session. Fortunately, there are ways to restore the original functionality without resorting to restarting your session.

Solution: Deleting the Masking Variable

To restore the original built-in function, simply delete the variable name that you used to overwrite it. For example, if you accidentally set set as a variable, run the following command:

<code class="python">del set</code>

Now, set will refer to the original built-in set function.

Alternative: Accessing Built-Ins Through the Builtins Module

If you want to use the original built-in function from your overwritten function, you can access it through the builtins module. In Python 3, this module is called builtins, while in Python 2, it's called __builtin__. For example, if you overwrote set but want to use the original one, you can do:

<code class="python">import builtins
original_set = builtins.set</code>

Determining the Masking Namespace

If you're unsure where the masking name is defined, check the namespaces from your current one up to the built-ins. Use the scoping rules described in "Short description of the scoping rules?" to determine which scopes may be relevant.

The above is the detailed content of Overwrote a Built-in Function? How Do I Get It Back?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn