问题描述
在某处(或者更好的是,一个模块!)是否有列表,我可以用来检查字符串是否是变量名的坏"选择,其中坏"被定义为诸如是关键字"之类的东西或内置功能等."
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."?
我有一个脚本,可以从Jinja模板(准确地说是Django模型)生成Python类,并且我想修复由于上述原因而不合适的任何字段名.
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!
推荐答案
您可能想检查__builtins__
键:
>>> __builtins__.keys()
dict_keys(['AttributeError', 'FloatingPointError', 'NotADirectoryError', 'UnicodeWarning', 'vars', 'delattr', 'chr', 'classmethod', 'iter', 'issubclass', 'isinstance', 'SyntaxWarning', 'SystemError', 'UnicodeError', '__spec__', 'UnboundLocalError', 'filter', 'FileNotFoundError', 'bin', 'frozenset', 'IndexError', 'property', 'type', 'credits', 'next', 'print', '__debug__', 'zip', 'LookupError', 'str', 'int', '__package__', 'hash', 'ArithmeticError', 'all', 'AssertionError', 'EOFError', 'input', 'IsADirectoryError', 'ConnectionResetError', 'ZeroDivisionError', 'max', 'TypeError', 'map', 'round', 'dir', 'license', 'EnvironmentError', 'KeyError', 'UserWarning', 'NameError', 'BytesWarning', 'UnicodeDecodeError', 'compile', 'sorted', 'Exception', 'min', 'ResourceWarning', 'bytearray', 'DeprecationWarning', 'help', 'NotImplemented', 'NotImplementedError', 'ValueError', 'ReferenceError', 'PendingDeprecationWarning', 'PermissionError', '_', 'divmod', 'open', 'MemoryError', 'any', 'bytes', 'ProcessLookupError', 'InterruptedError', 'enumerate', 'FileExistsError', 'complex', 'IOError', 'UnicodeTranslateError', 'Ellipsis', 'abs', 'GeneratorExit', 'quit', 'pow', 'reversed', 'ascii', 'ord', '__build_class__', 'globals', 'float', 'bool', '__name__', 'ImportWarning', 'FutureWarning', 'StopIteration', 'hex', 'None', 'super', 'RuntimeError', '__doc__', 'KeyboardInterrupt', 'eval', 'tuple', 'exec', 'RuntimeWarning', 'ConnectionAbortedError', 'TimeoutError', 'memoryview', 'hasattr', 'BufferError', 'dict', 'setattr', 'set', 'BaseException', '__loader__', 'ConnectionError', 'False', 'OSError', 'TabError', 'OverflowError', 'repr', 'WindowsError', 'staticmethod', 'list', 'oct', 'Warning', 'id', 'SystemExit', '__import__', 'callable', 'UnicodeEncodeError', 'SyntaxError', 'locals', 'getattr', 'len', 'exit', 'range', 'IndentationError', 'ImportError', 'object', 'ConnectionRefusedError', 'BlockingIOError', 'slice', 'copyright', 'ChildProcessError', 'sum', 'format', 'True', 'BrokenPipeError'])
这篇关于Python中要避免的变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!