有没有办法保护python中的内置函数? [英] Is there a way to protect built-ins in python?

查看:92
本文介绍了有没有办法保护python中的内置函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题来自这个问题,用户在不知不觉中重新绑定而感到困惑内置的全局 set .当您尝试覆盖内置函数时,有没有一种简单的方法可以让python警告您?

My question arises from this question, in which a user got himself confused by unknowingly rebinding the built-in global set. Is there a straightforward way to get python to warn you when you attempt to override a built-in?

我正在更具体地考虑Mathematica.在Mathematica中,所有内置函数都具有 Protected 属性.如果您尝试重新定义 Set ,它将告诉您 Set Protected 并将其保留不变,这可能会在以后使您免于遭受大量混乱应该工作的事情没有明显的原因就停止了.如果您真的很想重新定义 Set ,您仍然可以这样做–您只需要先 Unprotect 即可.而且,您也可以保护自己的符号,以防止它们被意外重新定义.

I'm thinking more specifically of Mathematica. In Mathematica all built-ins have attribute Protected. If you try to redefine Set, it will tell you that Set is Protected and leave it unchanged, possibly saving you from massive confusion later on when things that ought to work stop doing so for no obvious reason. If you really seriously want to redefine Set you can still do it -- you just have to Unprotect it first. And you can Protect your own symbols, too, to protect them from accidental redefinition.

经验丰富的pythoner也许不需要这个,但是如果像我这样的新手有这样的东西,那就太好了.(想一想,我已经对Mathematica进行了15年的编程,而 Protected 仍然偶尔会节省我的培根.)

Perhaps experienced pythoners don't need this, but it would be nice if there were something like this for newbies like me. (Come to think of it, I've been programming Mathematica for 15 years and Protected still occasionally saves my bacon.)

推荐答案

使用 http://www.pylint.org/

def set():
    pass

将生成此警告:

[W0622,set]重新定义内置的"set"

[W0622, set] Redefining built-in 'set'

您可以在编写文件后在文件上运行它,也可以在例如Vim随身携带.大多数IDE也会生成此类信息

You can run it over your files after you write them, or use it in e.g. Vim as you go. Most IDE's will also generate this sort of information

这篇关于有没有办法保护python中的内置函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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