Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to protect built-ins in 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?

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.

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.)

like image 998
Leon Avery Avatar asked Nov 28 '14 14:11

Leon Avery


1 Answers

Use http://www.pylint.org/

def set():
    pass

Will generate this warning:

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

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

like image 127
Paul Collingwood Avatar answered Oct 14 '22 23:10

Paul Collingwood