如何取回被覆盖的python内置函数? [英] How to get back an overridden python built-in function?

查看:117
本文介绍了如何取回被覆盖的python内置函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在探索 StackOverflow 问题的解决方案时,Python 使用用户定义的字符串类,我带来了这种奇怪的 Python 行为.

When I was exploring a solution for the StackOverflow problem, Python Use User Defined String Class, I came with this strange python behavior.

def overriden_print(x):
    print "Overriden in the past!"

from __future__ import print_function

print = overriden_print

print("Hello World!")

输出:

覆盖过去!

现在,我怎样才能在 python 解释器中恢复原来的 print 行为?

Now, how can I get back the original print behavior in python interpreter?

推荐答案

只需删除覆盖:

del print

这会从 globals() 字典中删除名称,让搜索回退到内置函数.

This deletes the name from the globals() dictionary, letting search fall back to the built-ins.

您始终可以通过 __builtin__模块以及:

You can always refer directly to the built-in via the __builtin__ module as well:

import __builtin__

__builtin__.print('Printing with the original built-in')

在 Python 3 中,模块已重命名为 builtins.

In Python 3, the module has been renamed to builtins.

这篇关于如何取回被覆盖的python内置函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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