for loop without variable

E

erik gartz

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.
 
F

Fredrik Lundh

erik said:
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>
 
D

Diez B. Roggisch

erik said:
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
 
T

Tim Chase

Hi. I'd like to be able to write a loop such as:
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
 
T

Thomas Heller

erik said:
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
 
B

Ben Finney

Diez B. Roggisch said:
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.
 
J

John Machin

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 ...
 
M

MRAB

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
 
D

Dan Sommers

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
 
E

erik gartz

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

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.:)
 
B

Basilisk96

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.

Do you think you could apply something like this:

def foo():print "fetching foo..."
actions = (foo,)*5
for f in actions:
f()

fetching foo...
fetching foo...
fetching foo...
fetching foo...
fetching foo...

...but not knowing your specific implementation, I may be off the wall
here.

Cheers,
-Basilisk96
 
C

Carl Banks

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.


IIRC, in pylint you can turn off checking for a particular symbol. I had
to edit a .pylintrc file (location may vary on Windows) and there was a
declaration in the file that listed symbols to ignore.

Last time I bothered running it, I added "id" to that list, since I use
it often (bad habit) and almost never use the builtin id, but still
wanted shadowing warnings for other symbols.


Carl Banks
 
B

Ben Finney

erik gartz said:
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.

When you have iteration requirements that don't seem to fit the
built-in types (lists, dicts, generators etc.), turn to 'itertools'
... import random
... print "Trying ..."
... success = random.choice([True, False])
... return success
... ... if foo_attempt():
... break
...
Trying ...
Trying ...
Trying ...
Trying ...
Trying ...
Trying ...
Note that this is possibly more readable than 'for foo_attempt in
[foo] * max_attempts", and is more efficient for large values of
'max_attempts' because 'repeat' returns an iterator instead of
actually allocating the whole sequence.
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 think your intent -- "repeat this operation N times" -- is better
expressed by the above code, than by keeping count of something you
don't actually care about.
I don't want to turn the warning off because catching unused
variables in the general is useful to me.

Agreed.
 
M

Mike Meyer

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.

It sounds to me like your counter variable actually has meaning, but
you've hidden that meaning by giving it the meaningless name "i". If
you give it a meaningful name, then there's an obvious way to do it
(which you listed yourself):

while retries_left:
if this_succeeds():
return True
retries_left -= 1

return False

<mike
 
H

Hrvoje Niksic

Mike Meyer said:
It sounds to me like your counter variable actually has meaning,

It depends how the code is written. In the example such as:

for meaningless_variable in xrange(number_of_attempts):
...

the loop variable really has no meaning. Rewriting this code only to
appease pylint is exactly that, it has nothing with making the code
more readable.
you've hidden that meaning by giving it the meaningless name "i". If
you give it a meaningful name, then there's an obvious way to do it
(which you listed yourself):

while retries_left:
[...]

This loop contains more code and hence more opportunities for
introducing bugs. For example, if you use "continue" anywhere in the
loop, you will do one retry too much.
 
S

Sam

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

wrong :)

# pylint: disable-msg=XXXXX will only impact the current line!

$ cat -n dummy.py
1 for i in range(2): # pylint: disable-msg=W0612
2 print "foo"
3 for i in range(2):
4 print "foo"

pylint will not generate a warning on line 1, but will on line 3!

Cheers.

Sam
 
J

Jeroen Ruigrok van der Werven

-On [20080110 00:21] said:
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.

The same applies for Babel (http://babel.edgewall.org/) where we have _() in
similar vein to the gettext implementation.
 
T

Torsten Bronger

Hallöchen!

Ben said:
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.

Right, that's because I've used "__" where not all returning values
are interesing to me such as

a, b, __ = function_that_returns_three_values(x, y)

However, in loops, I prefer real names, even if the loop variable
isn't used outside.

Tschö,
Torsten.
 
G

Guest

Torsten said:
Hallöchen!



Right, that's because I've used "__" where not all returning values
are interesing to me such as

a, b, __ = function_that_returns_three_values(x, y)

Variable name "dummy" serves the same purpose, such as:

a, b, dummy = function_that_returns_three_values(x, y)

According to http://linux.die.net/man/1/pylint it is also possible to use the
option

--dummy-variables-rgx=<regexp>

to further specify which variable not to report as unused. As far as I can
tell, it defaults to '_|dummy'.

..david
 
D

Duncan Booth

erik gartz said:
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.


Google for: pylint unused

It pointed me at:
Question:
I've a function / method which is a callback where I do not have any
control on received argument, and pylint is complaining about unused
arguments. What can I do to avoid those warnings ?

Answer:
prefix (ui) the callback's name by cb_, as in cb_onclick(...). By doing
so arguments usage won't be checked. Another solution is to use one of
the name defined in the "dummy-variables" configuration variable for
unused argument ("_" and "dummy" by default).

http://www.iaeste.or.at/doc/python2.3-pylint/html/FAQ.html

So it looks like you can use 'dummy' or add any other names you want to the
configuration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top