Is there a way to protect built-ins in python?

2024/9/8 10:15:01

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

Answer

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

https://en.xdnf.cn/q/72701.html

Related Q&A

Generate thumbnail for arbitrary audio file

I want to represent an audio file in an image with a maximum size of 180180 pixels.I want to generate this image so that it somehow gives a representation of the audio file, think of it like SoundCloud…

Extract specific text lines?

I have a large several hudred thousand lines text file. I have to extract 30,000 specific lines that are all in the text file in random spots. This is the program I have to extract one line at a time:b…

Listing users for certain DB with PyMongo

What Im trying to acheiveIm trying to fetch users for a certain database.What I did so farI was able to find function to list the databases or create users but none for listing the users, I thought ab…

Using python selenium for Microsoft edge

I am trying to use pythons selenium for Microsoft edge but I keep getting this error:WebDriverException: Message: unknown error: cannot find Microsoft Edge binaryI downloaded the latest version of the …

get all unicode variations of a latin character

E.g., for the character "a", I want to get a string (list of chars) like "aāăą" (not sure if that example list is complete...) (basically all unicode chars with names "Latin…

How do I install Django on Ubuntu 11.10?

Im using The Definitive guide to installing Django on ubuntu and ironically need something more definitive because I cant make it work.(I have followed the steps before this on the link above) Here is …

Sympy second order ode

I have a homogeneous solution to a simple second-order ODE, which when I try to solve for initial values using Sympy, returns the same solution. It should substitute for y(0) and y(0) and yield a solut…

Bulk update using Peewee library

Im trying to update many records inside a table using Peewee library. Inside a for loop, i fetch a single record and then I update it but this sounds awful in terms of performance so I need to do the u…

Can you specify variance in a Python type annotation?

Can you spot the error in the code below? Mypy cant.from typing import Dict, Anydef add_items(d: Dict[str, Any]) -> None:d[foo] = 5d: Dict[str, str] = {} add_items(d)for key, value in d.items():pr…

Django loaddata error

I created a "fixtures" folder in the app directory and put data1.json in there.This is what is in the file:[{"firm_url": "http://www.graychase.com/kadam", "firm_name&…