Does anyone else use this little idiom?

T

Troels Thomsen

for action in repeat(f, n): action()
I don't know how 'Pythonic' this would be...

agree,
or this:

import itertools

def f1():
print "hello"

[f() for f in itertools.repeat(f1,6)]


tpt
 
M

Marc 'BlackJack' Rintsch

Only until Python 3.0, since the 'xrange' implementation will become
'range' at that time.

The point wasn't `range` vs. `xrange` but the arguments (1,n) vs. (n).

Ciao,
Marc 'BlackJack' Rintsch
 
M

Marc 'BlackJack' Rintsch

Plain Python function are very often more powerful than classes:

... if not hasattr(go, 'count'):
... go.count = count
... if go.count <= 0:
... del go.count
... return False
... go.count -= 1
... return True
...
... print 'hello'
...
hello
hello
hello

Please try:

while go(3):
while go(3):
print 'Think about it...'

Ciao,
Marc 'BlackJack' Rintsch
 
P

Paul Rubin

where n is an integer representing how many times you want
to execute "some code." ... I tend to write it as:

for _ in xrange (1,n):
some code

But this does it n-1 times, not n times.
 
I

Ivan Illarionov

Please try:

while go(3):
while go(3):
print 'Think about it...'

Ciao,
Marc 'BlackJack' Rintsch
Please try:

while go(3):
while go(3):
print 'Think about it...'

This doesn't work with nested loops as well as Go(count) and again().
I use 'for i in range(3)'. IMO it's good to name an index variable
even if it's not used inside the loop - it has the noble purpose of
being a loop index and therefore it is not 'dummy' in any way :).
 
S

samwyse

My apologies if any attributions are messed up.

I'm with Steven here. I typically use 'dontcare' or 'ignore', or
occasionally 'asdf'. I also tend to use variables whose name-length is
proportional to the size of their scope.
I'd really
prefer something like the Ruby syntax because all you have to write is
the number of times you want to do something, and then the thing you
want to do. (I guess, in that respect, I'd even consider the xrange
call a redundancy.)

I'd like to see a '..' operator that does what 'xrange' does, but I'm
not going to hold my breath.
 
B

BJörn Lindqvist

In Python, the direct translation of this is a for loop. When the
index doesn't matter to me, I tend to write it as:

for _ in xrange (1,n):
some code

An alternative way of indicating that you don't care about the loop
index would be

for dummy in xrange (1,n):
some code

I usually use _ when I know that i18n doesn't matter. dummy is just to
long when unpacking sequences:

for dummy, email, dummy, dummy in persons:
sendmail(email)

for _, email, _, _ in persons:
sendmail(email)
 
B

Bob Martin

in 332496 20080204 102153 "=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= said:
I usually use _ when I know that i18n doesn't matter. dummy is just to
long when unpacking sequences:

for dummy, email, dummy, dummy in persons:
sendmail(email)

for _, email, _, _ in persons:
sendmail(email)

Rexx's method is the way to do it : "do 50"
 
S

Steven D'Aprano

Rexx's method is the way to do it : "do 50"

I tried writing Rexx code and executing it in Python, but I got
unexpected results, mostly SyntaxError exceptions. Is that a bug in
Python?



No-I'm-not-really-serious-ly yours,
 
N

Nick Craig-Wood

Ruby has a neat little convenience when writing loops where you don't
care about the loop index: you just do n.times do { ... some
code ... } where n is an integer representing how many times you want
to execute "some code."

In Python, the direct translation of this is a for loop. When the
index doesn't matter to me, I tend to write it as:

for _ in xrange (1,n):
some code

An alternative way of indicating that you don't care about the loop
index would be

for dummy in xrange (1,n):
some code

I use pychecker a lot. It views variables called [ '_', 'unused',
'empty', 'dummy' ] as names to ignore if they haven't been used.

So according to pychecker '_' and 'dummy' would both be OK.

As for me personally, I usually use '_' but sometimes use 'dummy'
depending on the surrounding code.

Note that this idiom is fairly common in python too

wanted, _, _, _, also_wanted = a_list

which looks quite neat to my eyes.
 
E

Eduardo O. Padoan

Ruby has a neat little convenience when writing loops where you don't
care about the loop index: you just do n.times do { ... some
code ... } where n is an integer representing how many times you want
to execute "some code."

In Python, the direct translation of this is a for loop. When the
index doesn't matter to me, I tend to write it as:

for _ in xrange (1,n):
some code

An alternative way of indicating that you don't care about the loop
index would be

for dummy in xrange (1,n):
some code

I use pychecker a lot. It views variables called [ '_', 'unused',
'empty', 'dummy' ] as names to ignore if they haven't been used.

So according to pychecker '_' and 'dummy' would both be OK.

As for me personally, I usually use '_' but sometimes use 'dummy'
depending on the surrounding code.

Note that this idiom is fairly common in python too

wanted, _, _, _, also_wanted = a_list

which looks quite neat to my eyes.

BTW and FWIW, in Py3k you can do:

wanted, *_, also_wanted = a_list

http://www.python.org/dev/peps/pep-3132/
 

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

No members online now.

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top