473,778 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jan 9 '08 #1
35 21579
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
Jan 9 '08 #2
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

Jan 9 '08 #3
"Diez B. Roggisch" <de***@nospam.w eb.dewrites:
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
Jan 9 '08 #4
On Jan 9, 11:17 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
wrote:
"Diez B. Roggisch" <de...@nospam.w eb.dewrites:
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
Jan 10 '08 #5
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_o r_dictionary:
do_something_wi th(thing)

HTH,
Dan

--
Dan Sommers A death spiral goes clock-
<http://www.tombstoneze ro.net/dan/ wise north of the equator.
Atoms are not things. -- Werner Heisenberg -- Dilbert's PHB
Jan 10 '08 #6
On Jan 9, 8:35 pm, Dan Sommers <m...@privacy.n etwrote:
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_o r_dictionary:
do_something_wi th(thing)

HTH,
Dan

--
Dan Sommers A death spiral goes clock-
<http://www.tombstoneze ro.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.:-)
Jan 10 '08 #7
On Jan 9, 9:49 pm, erik gartz <eegun...@yahoo .comwrote:
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
Jan 10 '08 #8
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. 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
Jan 10 '08 #9
erik gartz <ee******@yahoo .comwrites:
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'
<URL:http://www.python.org/doc/lib/module-itertoolsin the standard
library.
>>from itertools import repeat
>>def foo():
... import random
... print "Trying ..."
... success = random.choice([True, False])
... return success
...
>>max_attempt s = 10
for foo_attempt in repeat(foo, max_attempts):
... 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.

--
\ "Dyslexia means never having to say that you're ysror." |
`\ —anonymous |
_o__) |
Ben Finney
Jan 10 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7386
by: zeroDoNotYeSpamtype | last post by:
(I tried to post this earlier, but it seems without success) A similar question to this has been answered many times, to whit the question applying when the index variable of a for loop is declared in the loop header, but this time that isn't the case: I've got a piece of code a bit like this: #include <iostream> #include <cmath>
12
2094
by: Danny Colligan | last post by:
In the following code snippet, I attempt to assign 10 to every index in the list a and fail because when I try to assign number to 10, number is a deep copy of the ith index (is this statement correct?). .... number = 10 .... So, I have to resort to using enumerate to assign to the list:
6
13766
by: Bora Ji | last post by:
Please help me to creating dynamic VARIABLE in java, with predefined name.
3
2812
by: dowlingm815 | last post by:
I am receiving a Do While Syntax Error - Loop without Do - Can't See it. I would appreciate any fresh eyes? Mary Private Sub Create_tblNJDOC() On Error GoTo Err_Hndlr
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.