473,789 Members | 2,893 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
35 21585
On Wed, 9 Jan 2008 18:49:36 -0800 (PST) erik gartz <ee******@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.
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
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
Jan 10 '08 #11
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


Jan 10 '08 #12
Mike Meyer <mw************ ***********@mir ed.orgwrites:
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_var iable in xrange(number_o f_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.
Jan 10 '08 #13
-On [20080110 00:21], Ben Finney (bi************ ****@benfinney. id.au) wrote:
>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.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org/ asmodai
イェルーン ラウフãƒ*ッ ク ヴァン デル ウェルヴェ ン
http://www.in-nomine.org/ | http://www.rangaku.org/
With a nuclear fire of Love in our Hearts, rest easy baby, rest easy...
Jan 10 '08 #14
Hallöchen!

Ben Finney writes:
"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.
Right, that's because I've used "__" where not all returning values
are interesing to me such as

a, b, __ = function_that_r eturns_three_va lues(x, y)

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

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber. org
(See http://ime.webhop.org for further contact info.)
Jan 10 '08 #15
Torsten Bronger wrote:
>Hallöchen!

Ben Finney writes:
>"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.

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

a, b, __ = function_that_r eturns_three_va lues(x, y)
Variable name "dummy" serves the same purpose, such as:

a, b, dummy = function_that_r eturns_three_va lues(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
Jan 10 '08 #16
erik gartz <ee******@yahoo .comwrote:
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..../html/FAQ.html

So it looks like you can use 'dummy' or add any other names you want to the
configuration.
Jan 10 '08 #17
Hallöchen!

Da***********@s weco.no writes:
Torsten Bronger wrote:
>[...]

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

a, b, __ = function_that_r eturns_three_va lues(x, y)

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

a, b, dummy = function_that_r eturns_three_va lues(x, y)
Granted, but my rationale is that "__" is less visible in the source
code, so there is more emphasis on the actually interesting
variables.

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber. org
(See http://ime.webhop.org for further contact info.)
Jan 10 '08 #18
Torsten Bronger writes:
>Da***********@ sweco.no writes:
>Torsten Bronger wrote:
>>[...]

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

a, b, __ = function_that_r eturns_three_va lues(x, y)

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

a, b, dummy = function_that_r eturns_three_va lues(x, y)

Granted, but my rationale is that "__" is less visible in the source
code, so there is more emphasis on the actually interesting
variables.
I guess it's a matter of preference. Personally, I find "dummy" to be more
explicit, and hence more readable for those that that will read my code
later. YMMV.

Regards,
..david
Jan 10 '08 #19
On Thu, 10 Jan 2008 08:42:16 +0100 Hrvoje Niksic <hn*****@xemacs .orgwrote:
Mike Meyer <mw************ ***********@mir ed.orgwrites:
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_var iable in xrange(number_o f_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.
Except in this case, the variable *has* a meaning. You've just chosen
to obfuscate it.
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.
All correct - and I'm a big fan of minimizing code, as code you don't
write has no bugs in it. But you can still show the meaning of this
"meaningles s" variable:

for number_of_attem pts in xrange(maximum_ attempts):

Of course, the OP's request is a better solution: since he doesn't
actually need the variable, removing it completely means there's one
less variable, which is one less thing you can set to the wrong value.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
Jan 10 '08 #20

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

Similar topics

3
7387
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
13770
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
10199
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...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
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...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6768
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
5417
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...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
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
3
2909
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.