I am writing for _, myvar2 in mylist
, when I want to emphasize that only myvar2 is used.
And I write for myvar1, myvar2 in mylist
when I want to remind the reader (usually me) what a record structure is.
The name _
is just a naming convention for the throw value. The CPython interpreter does the assignment of a variable for it in the same way as with any other variable name (fortunately, * STORE_FAST * is a very cheap operation). In contrast, the PyPy interpreter identifies the unused variable assignment as dead code, so you get free optimization no matter how you write it.
If you are interested in how CPython interprets your code, dis module can provide useful information:
>>> from dis import dis >>> def f(lot): for _, var2 in lot: print var2 >>> dis(f) 2 0 SETUP_LOOP 25 (to 28) 3 LOAD_FAST 0 (lot) 6 GET_ITER >> 7 FOR_ITER 17 (to 27) 10 UNPACK_SEQUENCE 2 13 STORE_FAST 1 (_) 16 STORE_FAST 2 (var2) 3 19 LOAD_FAST 2 (var2) 22 PRINT_ITEM 23 PRINT_NEWLINE 24 JUMP_ABSOLUTE 7 >> 27 POP_BLOCK >> 28 LOAD_CONST 0 (None) 31 RETURN_VALUE
As other posters noted, warnings from pylint can sometimes be blank. If you prefer the short variable name in your code, just ignore the pylint complaint. As Francis Avila noted, pilint should not complain about _
in this context.
source share