This question already has an answer here:
Is there a list somewhere (or better yet, a module!) that I can use to check whether a string is a "bad" choice for a variable name, where "bad" is defined as something like "is a keyword or built-in function etc."?
I have a script that generates Python classes from a Jinja template (Django models to be precise) and I'd like to fix any field names that are unsuitable for reasons like the ones I mentioned above.
So far, I've got a check that looks like this:
def is_bad_name(name):
return keyword.iskeyword(name) or (name in ["type"])
So another way of phrasing my question would be: what else should I put in that list along with "type" ?
I realize that there can't be any complete list since it will vary depending on what is defined in other modules I'm using, but I wonder if there is a good list of things that should pretty much never be used. Thanks!
You might want to add
__builtins__.__dict__.keys()
andsys.builtin_module_names
to that listYou probably want to check against
__builtins__
keys: