Skip to content Skip to sidebar Skip to footer

Variable Names To Avoid In Python

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

Solution 1:

You probably want to check against __builtins__ keys:

>>> __builtins__.keys()

['__name__',
 '__doc__',
 '__package__',
 '__loader__',
 '__spec__',
 '__build_class__',
 '__import__',
 'abs',
 'all',
 'any',
 'ascii',
 'bin',
 'callable',
 'chr',
 'compile',
 'delattr',
 'dir',
 'divmod',
 'eval',
 'exec',
 'format',
 'getattr',
 'globals',
 'hasattr',
 'hash',
 'hex',
 'id',
 'input',
 'isinstance',
 'issubclass',
 'iter',
 'len',
 'locals',
 'max',
 'min',
 'next',
 'oct',
 'ord',
 'pow',
 'print',
 'repr',
 'round',
 'setattr',
 'sorted',
 'sum',
 'vars',
 'None',
 'Ellipsis',
 'NotImplemented',
 'False',
 'True',
 'bool',
 'memoryview',
 'bytearray',
 'bytes',
 'classmethod',
 'complex',
 'dict',
 'enumerate',
 'filter',
 'float',
 'frozenset',
 'property',
 'int',
 'list',
 'map',
 'object',
 'range',
 'reversed',
 'set',
 'slice',
 'staticmethod',
 'str',
 'super',
 'tuple',
 'type',
 'zip',
 '__debug__',
 'BaseException',
 'Exception',
 'TypeError',
 'StopAsyncIteration',
 'StopIteration',
 'GeneratorExit',
 'SystemExit',
 'KeyboardInterrupt',
 'ImportError',
 'ModuleNotFoundError',
 'OSError',
 'EnvironmentError',
 'IOError',
 'WindowsError',
 'EOFError',
 'RuntimeError',
 'RecursionError',
 'NotImplementedError',
 'NameError',
 'UnboundLocalError',
 'AttributeError',
 'SyntaxError',
 'IndentationError',
 'TabError',
 'LookupError',
 'IndexError',
 'KeyError',
 'ValueError',
 'UnicodeError',
 'UnicodeEncodeError',
 'UnicodeDecodeError',
 'UnicodeTranslateError',
 'AssertionError',
 'ArithmeticError',
 'FloatingPointError',
 'OverflowError',
 'ZeroDivisionError',
 'SystemError',
 'ReferenceError',
 'BufferError',
 'MemoryError',
 'Warning',
 'UserWarning',
 'DeprecationWarning',
 'PendingDeprecationWarning',
 'SyntaxWarning',
 'RuntimeWarning',
 'FutureWarning',
 'ImportWarning',
 'UnicodeWarning',
 'BytesWarning',
 'ResourceWarning',
 'ConnectionError',
 'BlockingIOError',
 'BrokenPipeError',
 'ChildProcessError',
 'ConnectionAbortedError',
 'ConnectionRefusedError',
 'ConnectionResetError',
 'FileExistsError',
 'FileNotFoundError',
 'IsADirectoryError',
 'NotADirectoryError',
 'InterruptedError',
 'PermissionError',
 'ProcessLookupError',
 'TimeoutError',
 'open',
 'quit',
 'exit',
 'copyright',
 'credits',
 'license',
 'help',
 '_']
>>> pprint.pprint(list(a))
['__name__',
 '__doc__',
 '__package__',
 '__loader__',
 '__spec__',
 '__build_class__',
 '__import__',
 'abs',
 'all',
 'any',
 'ascii',
 'bin',
 'callable',
 'chr',
 'compile',
 'delattr',
 'dir',
 'divmod',
 'eval',
 'exec',
 'format',
 'getattr',
 'globals',
 'hasattr',
 'hash',
 'hex',
 'id',
 'input',
 'isinstance',
 'issubclass',
 'iter',
 'len',
 'locals',
 'max',
 'min',
 'next',
 'oct',
 'ord',
 'pow',
 'print',
 'repr',
 'round',
 'setattr',
 'sorted',
 'sum',
 'vars',
 'None',
 'Ellipsis',
 'NotImplemented',
 'False',
 'True',
 'bool',
 'memoryview',
 'bytearray',
 'bytes',
 'classmethod',
 'complex',
 'dict',
 'enumerate',
 'filter',
 'float',
 'frozenset',
 'property',
 'int',
 'list',
 'map',
 'object',
 'range',
 'reversed',
 'set',
 'slice',
 'staticmethod',
 'str',
 'super',
 'tuple',
 'type',
 'zip',
 '__debug__',
 'BaseException',
 'Exception',
 'TypeError',
 'StopAsyncIteration',
 'StopIteration',
 'GeneratorExit',
 'SystemExit',
 'KeyboardInterrupt',
 'ImportError',
 'ModuleNotFoundError',
 'OSError',
 'EnvironmentError',
 'IOError',
 'WindowsError',
 'EOFError',
 'RuntimeError',
 'RecursionError',
 'NotImplementedError',
 'NameError',
 'UnboundLocalError',
 'AttributeError',
 'SyntaxError',
 'IndentationError',
 'TabError',
 'LookupError',
 'IndexError',
 'KeyError',
 'ValueError',
 'UnicodeError',
 'UnicodeEncodeError',
 'UnicodeDecodeError',
 'UnicodeTranslateError',
 'AssertionError',
 'ArithmeticError',
 'FloatingPointError',
 'OverflowError',
 'ZeroDivisionError',
 'SystemError',
 'ReferenceError',
 'BufferError',
 'MemoryError',
 'Warning',
 'UserWarning',
 'DeprecationWarning',
 'PendingDeprecationWarning',
 'SyntaxWarning',
 'RuntimeWarning',
 'FutureWarning',
 'ImportWarning',
 'UnicodeWarning',
 'BytesWarning',
 'ResourceWarning',
 'ConnectionError',
 'BlockingIOError',
 'BrokenPipeError',
 'ChildProcessError',
 'ConnectionAbortedError',
 'ConnectionRefusedError',
 'ConnectionResetError',
 'FileExistsError',
 'FileNotFoundError',
 'IsADirectoryError',
 'NotADirectoryError',
 'InterruptedError',
 'PermissionError',
 'ProcessLookupError',
 'TimeoutError',
 'open',
 'quit',
 'exit',
 'copyright',
 'credits',
 'license',
 'help',
 '_']

Solution 2:

You might want to add __builtins__.__dict__.keys() and sys.builtin_module_names to that list

Post a Comment for "Variable Names To Avoid In Python"