for loop without variable - Python
This is a discussion on for loop without variable - Python ; Hallöchen!
David.Reksten@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_returns_three_values(x, y)
>
> ...
-
Re: Conventions for dummy name
Hallöchen!
David.Reksten@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_returns_three_values(x, y)
>
> Variable name "dummy" serves the same purpose, such as:
>
> a, b, dummy = function_that_returns_three_values(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: bronger@jabber.org
(See http://ime.webhop.org for further contact info.)
-
SV: Conventions for dummy name
Torsten Bronger writes:
>David.Reksten@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_returns_three_values(x, y)
>>
>> Variable name "dummy" serves the same purpose, such as:
>>
>> a, b, dummy = function_that_returns_three_values(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
-
Re: for loop without variable
On Thu, 10 Jan 2008 08:42:16 +0100 Hrvoje Niksic <hniksic@xemacs.org> wrote:
> Mike Meyer <mwm-keyword-python.b4bdba@mired.org> writes:
> > 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.
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
"meaningless" variable:
for number_of_attempts 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 <mwm@mired.org> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
-
Re: for loop without variable
On Jan 9, 10:55 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
> erik gartz <eegun...@yahoo.com> writes:
> > 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-itertools> in the standard
> library.
>
> >>> from itertools import repeat
>
> >>> def foo():
> ... import random
> ... print "Trying ..."
> ... success = random.choice([True, False])
> ... return success
> ...
> >>> max_attempts = 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
Neat! That is the best solution I've seen so far. I should definitely
dig into the itertools module more often.
Cheers,
-Basilisk96
-
Re: for loop without variable
Hrvoje Niksic wrote:
> Mike Meyer <mwm-keyword-python.b4bdba@mired.org> writes:
>
>> 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.
I recently faced a similar issue doing something like this:
data_out = []
for i in range(len(data_in)):
data_out.append([])
This caused me to wonder why Python does not have a "foreach" statement (and
also why has it not come up in this thread)? I realize the topic has probably
been beaten to death in earlier thread(s), but does anyone have the short answer?
-
RE: for loop without variable
> On Behalf Of Marty
> I recently faced a similar issue doing something like this:
>
> data_out = []
> for i in range(len(data_in)):
> data_out.append([])
>
> This caused me to wonder why Python does not have a "foreach"
> statement (and also why has it not come up in this thread)?
> I realize the topic has probably been beaten to death in
> earlier thread(s), but does anyone have the short answer?
data_out = [[] for item in data_in]
Regards,
Ryan Ginstrom
-
Re: for loop without variable
On Thu, 10 Jan 2008 22:36:56 -0500 Marty <martyb1@earthlink.net> wrote:
> I recently faced a similar issue doing something like this:
>
> data_out = []
> for i in range(len(data_in)):
> data_out.append([])
More succinctly:
data_out = []
for _ in data_in:
data_out.append([])
Or, as has already been pointed out:
data_out = [[] for _ in data_in]
> This caused me to wonder why Python does not have a "foreach" statement (and
> also why has it not come up in this thread)? I realize the topic has probably
> been beaten to death in earlier thread(s), but does anyone have the short answer?
But I'm curious - what's the difference between the "foreach" you have
in mind and the standard python "for"?
<mike
--
Mike Meyer <mwm@mired.org> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
-
Re: for loop without variable
On Jan 10, 10:36 pm, Marty <mart...@earthlink.net> wrote:
> Hrvoje Niksic wrote:
> > Mike Meyer <mwm-keyword-python.b4b...@mired.org> writes:
>
> >> 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.
>
> I recently faced a similar issue doing something like this:
>
> data_out = []
> for i in range(len(data_in)):
> data_out.append([])
>
> This caused me to wonder why Python does not have a "foreach" statement (and
> also why has it not come up in this thread)? I realize the topic has probably
> been beaten to death in earlier thread(s), but does anyone have the short answer?
Pythons `for' essentially is foreach. The code below does the same
thing as what you have posted does. Actually, I've found that if you
find yourself ever doing range(len(data_in)) in python, it is time to
take a second look.
data_out = []
for x in data_in:
data_out.append([])
`range' is just a function that returns a list.
Matt
-
Re: for loop without variable
erik gartz <eegunnar@yahoo.com> writes:
> 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.
This uses an index var, but doesn't leak it outside the genexp, so
I don't know what pylint would say (untested):
def f():
return any(attempt_service() for i in xrange(10))
I think the above is pretty natural. If you really insist on not
using any variables, the below might work (untested):
from itertools import imap, repeat
def f():
return any(imap(apply, repeat(attempt_service, 10)))
it just seems way too obscure though. Python style seems to favor
spewing extra variables around.
-
Re: for loop without variable
Mike Meyer <mwm-keyword-python.b4bdba@mired.org> writes:
> data_out = [[] for _ in data_in]
> ...
> But I'm curious - what's the difference between the "foreach" you have
> in mind and the standard python "for"?
The "for" loop, like the list comprehension, pollutes the namespace
with an index variable that's not used for anything. I prefer genexps:
data_out = list([] for x in data_in)