A comprehensive review of strings that I should NOT use for local variables?

If I understood correctly, it is better not to use an expression for a local variable, which is already a global function in Python. Therefore, I believe that this

list = [1,2,3] 

not recommended in favor

 mylist = [1,2,3] 

because list already an inline object in Python, but mylist is not. However, I am not always sure whether I should use or not use any expression (e.g. dir , num or cnt ). Is there any comprehensive overview of strings that I should better avoid to indicate local variables?

+4
source share
2 answers

Names that should be avoided are keywords (which will give you an error, so they are easy to notice) and inline elements that will be covertly masked. Here is a code snippet for checking bad names:

 from keyword import kwlist def bad_name(name): return name in dir(__builtins__) + kwlist 

... and here is the list (for Python 3.3):

Built-in functions, types, etc.

 abs all any ascii bin bool bytearray bytes callable chr classmethod compile complex copyright credits delattr dict dir divmod enumerate eval exec exit filter float format frozenset getattr globals hasattr hash help hex id input int isinstance issubclass iter len license list locals map max memoryview min next object oct open ord pow print property quit range repr reversed round set setattr slice sorted staticmethod str sum super tuple type vars zip 

Everything that CamelCase contains (for example, built-in exceptions) or begins with double underscores is excluded from the list above, since you should not use it anyway.

Keywords

 False None True and as assert break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield 
+3
source

In principle, avoid all of these . All of them are inside the __builtin__ module ( builtins in Python 3).

Source: Python Standard Library Built-in Functions .

+5
source

Source: https://habr.com/ru/post/1445188/


All Articles