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
for action in repeat(f, n): action()
I don't know how 'Pythonic' this would be...
Only until Python 3.0, since the 'xrange' implementation will become
'range' at that time.
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
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
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...'
My apologies if any attributions are messed up.
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.)
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
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"
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
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.
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.