for loop without variable - Python

This is a discussion on for loop without variable - Python ; Hi. I'd like to be able to write a loop such as: for i in range(10): pass but without the i variable. The reason for this is I'm using pylint and it complains about the unused variable i. I can ...

+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 39

for loop without variable

  1. Default for loop without variable

    Hi. I'd like to be able to write a loop such as:
    for i in range(10):
    pass
    but without the i variable. The reason for this is I'm using pylint
    and it complains about the unused variable i. I can achieve the above
    with more lines of code like:
    i = 0
    while (i != 10):
    i += 1
    Is there a concise way to accomplish this without adding extra lines
    of codes? Thanks in advance for your help.

  2. Default Re: for loop without variable

    erik gartz wrote:

    > Hi. I'd like to be able to write a loop such as:
    > for i in range(10):
    > pass
    > but without the i variable. The reason for this is I'm using pylint
    > and it complains about the unused variable i.


    if a computer tells you to do something stupid, it's often better to
    find a way to tell the computer to shut up than to actually do some-
    thing stupid.

    (pylint's online documentation is remarkably unreadable, so I cannot
    help you with the details, but surely there's a way to disable that
    test, either globally, or for a specific region of code?).

    </F>


  3. Default Re: for loop without variable

    erik gartz schrieb:
    > Hi. I'd like to be able to write a loop such as:
    > for i in range(10):
    > pass
    > but without the i variable. The reason for this is I'm using pylint
    > and it complains about the unused variable i. I can achieve the above
    > with more lines of code like:
    > i = 0
    > while (i != 10):
    > i += 1
    > Is there a concise way to accomplish this without adding extra lines
    > of codes? Thanks in advance for your help.


    The underscore is used as "discarded" identifier. So maybe


    for _ in xrange(10):
    ...


    works.

    Diez

  4. Default Re: for loop without variable

    >> Hi. I'd like to be able to write a loop such as:
    >> for i in range(10):
    >> pass
    >> but without the i variable. The reason for this is I'm using pylint
    >> and it complains about the unused variable i.

    >
    > if a computer tells you to do something stupid, it's often better to
    > find a way to tell the computer to shut up than to actually do some-
    > thing stupid.
    >
    > (pylint's online documentation is remarkably unreadable, so I cannot
    > help you with the details, but surely there's a way to disable that
    > test, either globally, or for a specific region of code?).


    That documentation on PyLint[1] is atrocious!

    From my reading of it, at the beginning of your file, you put a
    comment something like

    # pylint: disable-msg=W0612

    which should disable that particular message. Totally untested.

    I don't know if PyLint is smart enough, but if you don't use "i",
    you might use the common "throwaway" variable of "_":

    for _ in xrange(10):
    do_something()

    in which case it _might_ recognize that it's a throwaway variable
    and not warn you about it. At least that would be nice of it.
    But given the abusive nature of the documenation, I'm not sure
    that's the case

    -tkc
    [1]http://www.logilab.org/card/pylintfeatures



  5. Default Re: for loop without variable

    erik gartz schrieb:
    > Hi. I'd like to be able to write a loop such as:
    > for i in range(10):
    > pass
    > but without the i variable. The reason for this is I'm using pylint
    > and it complains about the unused variable i.


    Pychecker won't complain if you rename 'i' to '_', IIRC:

    for _ in range(10):
    pass

    Thomas


  6. Default Conventions for dummy name (was: for loop without variable)

    "Diez B. Roggisch" <deets@nospam.web.de> writes:

    > The underscore is used as "discarded" identifier. So maybe
    >
    > for _ in xrange(10):
    > ...


    The problem with the '_' name is that it is already well-known and
    long-used existing convention for an entirely unrelated purpose: in
    the 'gettext' i18n library, the '_' function to get the
    locally-translated version of a text string.

    Since the number of programs that need to use something like 'gettext'
    (and therefore use the '_' function) is likely only to increase, it
    seems foolish to set one's program up for a conflict with that
    established usage.

    I've seen 'dummy' used as a "don't care about this value" name in
    other Python code. That seems more readable, more explicit, and less
    likely to conflict with existing conventions.

    --
    \ "It is forbidden to steal hotel towels. Please if you are not |
    `\ person to do such is please not to read notice." -- Hotel |
    _o__) sign, Kowloon, Hong Kong |
    Ben Finney

  7. Default Re: for loop without variable

    On Jan 10, 10:00 am, Tim Chase <python.l...@tim.thechases.com> wrote:
    > >> Hi. I'd like to be able to write a loop such as:
    > >> for i in range(10):
    > >> pass
    > >> but without the i variable. The reason for this is I'm using pylint
    > >> and it complains about the unused variable i.

    >
    > > if a computer tells you to do something stupid, it's often better to
    > > find a way to tell the computer to shut up than to actually do some-
    > > thing stupid.

    >
    > > (pylint's online documentation is remarkably unreadable, so I cannot
    > > help you with the details, but surely there's a way to disable that
    > > test, either globally, or for a specific region of code?).

    >
    > That documentation on PyLint[1] is atrocious!
    >
    > From my reading of it, at the beginning of your file, you put a
    > comment something like
    >
    > # pylint: disable-msg=W0612
    >
    > which should disable that particular message. Totally untested.
    >
    > I don't know if PyLint is smart enough, but if you don't use "i",
    > you might use the common "throwaway" variable of "_":
    >
    > for _ in xrange(10):
    > do_something()
    >
    > in which case it _might_ recognize that it's a throwaway variable
    > and not warn you about it. At least that would be nice of it.
    > But given the abusive nature of the documenation, I'm not sure
    > that's the case
    >
    > -tkc
    > [1]http://www.logilab.org/card/pylintfeatures


    I didn't get as far as the allegedly "abusive" part. I followed the
    link that you gave, but no matter what I clicked on, it would go
    looking for an obviously incorrect URL like http://www.logilab.org/#messages-control-options
    and just go (slowly!) to the home page ...

  8. Default Re: Conventions for dummy name (was: for loop without variable)

    On Jan 9, 11:17 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
    wrote:
    > "Diez B. Roggisch" <de...@nospam.web.de> writes:
    >
    > > The underscore is used as "discarded" identifier. So maybe

    >
    > > for _ in xrange(10):
    > > ...

    >
    > The problem with the '_' name is that it is already well-known and
    > long-used existing convention for an entirely unrelated purpose: in
    > the 'gettext' i18n library, the '_' function to get the
    > locally-translated version of a text string.
    >
    > Since the number of programs that need to use something like 'gettext'
    > (and therefore use the '_' function) is likely only to increase, it
    > seems foolish to set one's program up for a conflict with that
    > established usage.
    >
    > I've seen 'dummy' used as a "don't care about this value" name in
    > other Python code. That seems more readable, more explicit, and less
    > likely to conflict with existing conventions.
    >

    Perhaps a "discarded" identifier should be any which is an underscore
    followed by digits.

    A single leading underscore is already used for "private" identifiers,
    but they are usually an underscore followed by what would be a valid
    identifier by itself, eg. "_exit".

    The convention would then be:

    2 underscores + valid_by_self + 2 underscores => special

    underscore + valid_by_self => private

    underscore + invalid_by_self => dummy

  9. Default Re: for loop without variable

    On Wed, 09 Jan 2008 14:25:36 -0800, erik gartz wrote:

    > Hi. I'd like to be able to write a loop such as:
    > for i in range(10):
    > pass
    > but without the i variable. The reason for this is I'm using pylint and
    > it complains about the unused variable i ...


    What does that loop do? (Not the loop you posted, but the "real" loop
    in your application.) Perhaps python has another way to express what
    you're doing.

    For example, if you're iterating over the elements of an array, or
    through the lines of a file, or the keys of a dictionary, the "in"
    operator may work better:

    for thing in array_or_file_or_dictionary:
    do_something_with(thing)

    HTH,
    Dan

    --
    Dan Sommers A death spiral goes clock-
    <http://www.tombstonezero.net/dan/> wise north of the equator.
    Atoms are not things. -- Werner Heisenberg -- Dilbert's PHB

  10. Default Re: for loop without variable

    On Jan 9, 8:35 pm, Dan Sommers <m...@privacy.net> wrote:
    > On Wed, 09 Jan 2008 14:25:36 -0800, erik gartz wrote:
    > > Hi. I'd like to be able to write a loop such as:
    > > for i in range(10):
    > > pass
    > > but without the i variable. The reason for this is I'm using pylint and
    > > it complains about the unused variable i ...

    >
    > What does that loop do? (Not the loop you posted, but the "real" loop
    > in your application.) Perhaps python has another way to express what
    > you're doing.
    >
    > For example, if you're iterating over the elements of an array, or
    > through the lines of a file, or the keys of a dictionary, the "in"
    > operator may work better:
    >
    > for thing in array_or_file_or_dictionary:
    > do_something_with(thing)
    >
    > HTH,
    > Dan
    >
    > --
    > Dan Sommers A death spiral goes clock-
    > <http://www.tombstonezero.net/dan/> wise north of the equator.
    > Atoms are not things. -- Werner Heisenberg -- Dilbert's PHB


    The loop performs some actions with web services. The particular
    iteration I'm on isn't important to me. It is only important that I
    attempt the web services that number of times. If I succeed I
    obviously break out of the loop and the containing function (the
    function which has the loop in it) returns True. If all attempts fail
    the containing loop returns False.

    I guess based on the replies of everyone my best bet is to leave the
    code the way it is and suck up the warning from pylint. I don't want
    to turn the warning off because catching unused variables in the
    general is useful to me. Unfortunately, I don't *think* I can shut the
    warning for that line or function off, only for the entire file.
    Pylint gives you a rating of your quality of code which I think is
    really cool. This is a great motivation and helps me to push to
    "tighten the screws". However it is easy to get carried away with your
    rating.:-)

+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast