如何恢复我意外覆盖的内置文件? [英] How to restore a builtin that I overwrote by accident?

查看:115
本文介绍了如何恢复我意外覆盖的内置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过在交互式python会话中将 set 用作变量名而意外覆盖了-有什么办法可以访问原始的 set 函数而不只是重新启动会话?

I accidentally overwrote set by using it as a variable name in an interactive python session - is there any way that I can get access to the original set function without just restarting my session?

(该会话中我有很多东西,我宁愿不必这样做,尽管当然可以,如果需要的话。)

(I have so much stuff in that session that I'd rather not have to do that, although of course I can if necessary.)

推荐答案

只需删除掩盖内建函数的名称:

Just delete the name that is masking the builtin:

>>> set = 'oops'
>>> set
'oops'
>>> del set
>>> set
<type 'set'>

您仍然可以始终通过 builtins 模块( __ builtin __ ,带下划线且没有 s );如果您想覆盖内置函数,但又想从覆盖中遵循原始格式,请使用此选项:

You can always still access the original built-in through the builtins module (__builtin__ on Python 2, with underscores and no s); use this if you want to override the built-in but want to defer to the original still from the override:

>>> import builtins
>>> builtins.set
<type 'set'>

如果无法找到定义屏蔽名称的位置,请检查当前名称空间中的所有名称空间到内置的;请参阅范围界定规则的简短说明?,了解适用于您当前情况的范围。

If you have trouble locating where the masking name is defined, do check all namespaces from your current one up to the built-ins; see Short description of the scoping rules? for what scopes may apply to your current situation.

这篇关于如何恢复我意外覆盖的内置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆