FAQ
Consider the following code:
for i in range(mylimit):
foo()
running pychecker gives me a
"""
Local variable (i) not used
"""
complaint.
If I use
for dummy in range(mylimit):
....
## or
for _ in range(mylimit):
....
I get no complaint from pychecker.
I would welcome comments on best practices for this issue.
NOTE: I see much on google regarding unused local variables,
however, doing a search for 'python _' hasn't proved fruitful.

I would like to see comments here specifically on the use for `range'

On a related note: from the python interpreter if I do
help(_)
I get
Help on bool object:

class bool(int)
bool(x) -> bool
......
I'd welcome comments on this as well.

:) I expect to be edified is so many ways, some
of them unexpected.

thanks
--
Tim
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com

Search Discussions

  • Philip Semanchuk at Jun 13, 2011 at 3:50 pm

    On Jun 13, 2011, at 11:37 AM, Tim Johnson wrote:

    NOTE: I see much on google regarding unused local variables,
    however, doing a search for 'python _' hasn't proved fruitful.
    Yes, Google's not good for searching punctuation. But 'python underscore dummy OR unused' might work better.
    On a related note: from the python interpreter if I do
    help(_)
    I get
    Help on bool object:

    class bool(int)
    bool(x) -> bool
    ......
    I'd welcome comments on this as well.
    In the Python interpreter, _ gives you the results of the last expression. When you first start the interpreter, _ is undefined.

    $ python
    help(_)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name '_' is not defined
    True
    True
    help(_)
    Help on bool object:

    class bool(int)
    bool(x) -> bool

    In your case when you asked for help(_), the last object you used must have been a bool.
    :) I expect to be edified is so many ways, some
    of them unexpected.
    That's the nice thing about this list!

    Hope this helps
    Philip
  • Chris Angelico at Jun 13, 2011 at 3:55 pm

    On Tue, Jun 14, 2011 at 1:37 AM, Tim Johnson wrote:
    On a related note: from the python interpreter if I do
    help(_)
    I get
    Help on bool object:

    class bool(int)
    ?| ?bool(x) -> bool
    ?......
    ?I'd welcome comments on this as well.
    _ is special to IDLE.

    1+2
    3
    _
    3

    It's the last result. So presumably when you did it, your last result
    was something boolean.
    sorted([random.randint(1,10) for i in range(10)])
    [1, 1, 2, 3, 4, 4, 5, 7, 8, 10]
    set(_)
    {1, 2, 3, 4, 5, 7, 8, 10}

    Can be quite handy, if you remember it's there. Otherwise, not so much. :)

    Chris Angelico
  • Steven D'Aprano at Jun 13, 2011 at 4:02 pm

    On Mon, 13 Jun 2011 07:37:02 -0800, Tim Johnson wrote:

    Consider the following code:
    [...]

    You know Tim, if you hadn't blocked my email address in a fit of pique
    over something that didn't even involve you, you would have seen my
    answer to your question on the tutor at python.org mailing list yesterday.

    http://code.activestate.com/lists/python-tutor/83236/

    Yes, I realise that Tim probably won't see this either. I'm just making a
    point.



    --
    Steven
  • Chris Angelico at Jun 13, 2011 at 4:28 pm

    On Tue, Jun 14, 2011 at 2:02 AM, Steven D'Aprano wrote:
    On Mon, 13 Jun 2011 07:37:02 -0800, Tim Johnson wrote:

    Consider the following code:
    [...]

    You know Tim, if you hadn't blocked my email address in a fit of pique
    over something that didn't even involve you, you would have seen my
    answer to your question on the tutor at python.org mailing list yesterday.

    http://code.activestate.com/lists/python-tutor/83236/

    Yes, I realise that Tim probably won't see this either. I'm just making a
    point.



    --
    Steven
    --
    http://mail.python.org/mailman/listinfo/python-list
    As he says, edification comes from a variety of odd sources... I
    didn't know the linting ignored a variable called 'dummy' or '_'.
    Worth knowing!

    ChrisA
  • Steven D'Aprano at Jun 13, 2011 at 4:15 pm

    On Tue, 14 Jun 2011 01:55:04 +1000, Chris Angelico wrote:
    On Tue, Jun 14, 2011 at 1:37 AM, Tim Johnson wrote:
    On a related note: from the python interpreter if I do
    help(_)
    I get
    Help on bool object:

    class bool(int)
    ?| ?bool(x) -> bool
    ?......
    ?I'd welcome comments on this as well.
    _ is special to IDLE.
    Not just IDLE. Also the vanilla Python command line interpreter. In fact,
    you can even find the code that controls it:

    help(sys.displayhook)

    http://docs.python.org/library/sys.html#sys.displayhook



    --
    Steven
  • Chris Angelico at Jun 13, 2011 at 4:41 pm

    On Tue, Jun 14, 2011 at 2:15 AM, Steven D'Aprano wrote:
    On Tue, 14 Jun 2011 01:55:04 +1000, Chris Angelico wrote:
    _ is special to IDLE.
    Not just IDLE. Also the vanilla Python command line interpreter. In fact,
    you can even find the code that controls it:
    Sorry, my bad! I should have said "to the Python interactive
    interpreter", the code that backs IDLE and the command line
    interpreter. But in any case, it's a feature of the interactive that
    doesn't apply to scripts.

    ChrisA
  • Tim Johnson at Jun 13, 2011 at 5:50 pm
    * Tim Johnson [110613 07:58]:
    :) I expect to be edified is so many ways, some
    of them unexpected.
    Thanks for all of the responses and for those which might come
    later. I'm going to stick with the convention of using a variable
    beginning with `dummy' and stick that in my snippet generator.
    cheers
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
  • Ben Finney at Jun 14, 2011 at 12:39 am

    Tim Johnson <tim at johnsons-web.com> writes:

    If I use
    for dummy in range(mylimit):
    ....
    ## or
    for _ in range(mylimit):
    ....
    I get no complaint from pychecker.
    I would welcome comments on best practices for this issue.
    I have argued in the past against overloading the name ?_? for this
    <URL:http://mail.python.org/pipermail/python-list/2008-January/523777.html>
    since it has other, unrelated, meanings already established.

    Other responses in that thread are also useful.

    --
    \ ?I find the whole business of religion profoundly interesting. |
    `\ But it does mystify me that otherwise intelligent people take |
    _o__) it seriously.? ?Douglas Adams |
    Ben Finney

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 13, '11 at 3:37p
activeJun 14, '11 at 12:39a
posts9
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase