Re: Does anyone else use this little idiom?

2008-02-05 Thread Eduardo O. Padoan
On Feb 5, 2008 1:30 PM, Nick Craig-Wood <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >  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/

-- 
http://www.advogato.org/person/eopadoan/
Bookmarks: http://del.icio.us/edcrypt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-05 Thread Nick Craig-Wood
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>  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.

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-04 Thread Steven D'Aprano
On Mon, 04 Feb 2008 15:08:44 +, Bob Martin wrote:

> 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, 


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-04 Thread Bob Martin
in 332496 20080204 102153 "=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=" <[EMAIL 
PROTECTED]> wrote:
>> 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)

Rexx's method is the way to do it : "do 50"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-04 Thread 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)


-- 
mvh Björn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread samwyse
[EMAIL PROTECTED] wrote:
> My apologies if any attributions are messed up.
> 
> On Feb 3, 1:28 am, Steven D'Aprano <[EMAIL PROTECTED]
> cybersource.com.au> wrote:
> 
>>If you want an explicit name, try a variation of "dontcare". Assuming
>>that you're an English speaker.

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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Ivan Illarionov
On Feb 4, 12:56 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Sun, 03 Feb 2008 05:33:16 -0800, Ivan Illarionov wrote:
> > Plain Python function are very often more powerful than classes:
>
>  def go(count):
> > ...   if not hasattr(go, 'count'):
> > ... go.count = count
> > ...   if go.count <= 0:
> > ... del go.count
> > ... return False
> > ...   go.count -= 1
> > ...   return True
> > ...
>  while go(3):
> > ...   print 'hello'
> > ...
> > hello
> > hello
> > hello
>
> 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 :).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Paul Rubin
[EMAIL PROTECTED] writes:
> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Marc 'BlackJack' Rintsch
On Sun, 03 Feb 2008 05:33:16 -0800, Ivan Illarionov wrote:

> Plain Python function are very often more powerful than classes:
> 
 def go(count):
> ...   if not hasattr(go, 'count'):
> ... go.count = count
> ...   if go.count <= 0:
> ... del go.count
> ... return False
> ...   go.count -= 1
> ...   return True
> ...
 while go(3):
> ...   print 'hello'
> ...
> hello
> hello
> hello


Please try:

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

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Marc 'BlackJack' Rintsch
On Sun, 03 Feb 2008 15:13:14 +1100, Ben Finney wrote:

> "Gabriel Genellina" <[EMAIL PROTECTED]> writes:
> 
>> Should be `for _ in xrange(n)` to match the Ruby example. Both
>> iterate n times.
> 
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Thomas Dybdahl Ahle

On Sat, 2008-02-02 at 18:03 -0800, [EMAIL PROTECTED] wrote:
> for _ in xrange (1,n):
>some code

I'd always use i for loop variables I don't know what to call. It stands
for iterator or something. In a nested loop the next variable would
simply be called j and so on.

I also tend to use _, but in cases like:
_, _, something, _ = somecall()
where I only need one variable from a tupple. I only use this if I'm
sure the other values will probably never be of any use for the code.

However a large problem with _ is, that it is also the standard name of
the translate function of e.g. gettext.
In gui code you'll often find widget.setText(_("Save")). If you have
renamed _ this will give you problems.

> 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 don't like dummy, as it is too long. I generelly spend more times
looking into long variable than short ones.

Regards, Thomas


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread miller . paul . w
My apologies if any attributions are messed up.

On Feb 3, 1:28 am, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> On Sun, 03 Feb 2008 15:08:34 +1100, Ben Finney wrote:
> >> But I like using _ because it's only 1 character and communicates well
> >> the idea "I don't care about this variable."
>
> > Not to me. As you noted, '_' is easily ambiguous. Explicit is better
> > than implicit; the name 'dummy' makes it much clearer.

Actually, "_" isn't ambiguous at all.  It's a perfectly well-defined
variable name.  It just seems most people don't know that, probably
because most people never get the urge to name a variable "_".

The whole reason I find myself wanting to use "_" is that "for _ in
xrange (n):" goes beyond explicit to redundant if you're not using the
index variable inside the loop.  Ruby's version is much better in this
respect, because every token matters.

> In
> fact, to me "dummy" implies that it holds a dummy value that will be
> replaced later with the actual value needed.
>
> If you want an explicit name, try a variation of "dontcare". Assuming
> that you're an English speaker.

That reminds me... I saw some code once that used a dummy variable
named "dont_give_a_shit".  I got a few seconds of giggles out of it,
at least. :-)

> People seem to forget that "explicit" just means that there's a
> convention that nearly everybody knows, and if you follow it, nearly
> everybody will know what you mean. Often that convention is nothing more
> than the meanings of words in whatever human language you're speaking,
> but it's still a convention.

Well, I'd argue that's only part of what "explicitness" means.  In
this specific use case, having to write the index variable isn't being
explicit; it's a simple redundancy.  Writing "_", or "dontcare" or
"dont_give_a_shit" doesn't seem more explicit at all.  It seems silly
to have to write it at all if I don't intend to use it.  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.)

Thanks, everyone for the replies. :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread miller . paul . w
On Feb 3, 11:20 am, Paul McGuire <[EMAIL PROTECTED]> wrote:

[... some code... some words ... more code, etc. ...]

> But this still seems like a lot of work to avoid "for x in range(n):".

I agree.  The point of me using "for _ in xrange (n)" isn't to avoid
the for loop at all.  What I wanted was a pythonic way to express only
the necessary components of the loop, like the Ruby version "n.times
do { stuff }"  does.  There's no explicit index in the Ruby code,
because you don't care about it.

Now, if you could monkeypatch built-ins, I'd *almost* consider adding
a .times method to integers. But, of course, monkeypatching is evil. :-
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread miller . paul . w
On Feb 3, 10:42 am, Zentrader <[EMAIL PROTECTED]> wrote:
> > Not to me.  If I read "for _ in ...", I wouldn't be quite sure what _ was.
> > Is it some magic piece of syntax I've forgotten about?  Or something new
> > added to language while I wasn't paying attention (I still consider most
> > stuff added since 1.5 to be new-fangled :-)).
>
> +1 to forgotten about
> +1 to new-fangled=added since 1.5   When 3000 comes out I'll have to
> break down and buy a new Python book.  Also, it's amazing how much
> posting space is spent here replying to someone who is too lazy to key
> in a variable name.  Damn!

Lol.  It isn't that I'm too lazy; it's just that the character "_"
looks like "-" to me, and, in the math class I'm taking right now
(category theory), "-" is one of the notations to indicate "whatever"
-- a metasyntactic placeholder, if you will.  (For example, Hom (C, -)
is the covariant hom functor from C -> Sets.  If this makes no sense
to you, don't worry about it.)

After this discussion, it seems that if I'm to write Python for public
consumption, I should prefer

for dummy in xrange (n):
do_stuff()

or one of the other suggestions instead. :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Paul McGuire
On Feb 2, 9:48 pm, Jeff Schwab <[EMAIL PROTECTED]> wrote:
> How [EMAIL PROTECTED] wrote:
> > 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."
>
> Would something like this be acceptable?  It still requires a loop
> variable, plus an extra line of code per loop, plus a one-time class
> definition (and import into each client module), and it's probably
> slower than "for dummy in range."  The syntax might be more inuitive
> than "dummy" or "_" in a for loop, though.
>
> class Go:
>      def __init__(self, count):
>          self.count = count
>
>      def again(self):
>          if self.count <= 0:
>              return False
>          self.count -= 1
>          return True
>
> go = Go(3)
> while go.again():
>      print "hello"- Hide quoted text -
>
> - Show quoted text -

Here's something similar, in a decorator:

def iterative(fn):
def times_(n):
i = 0
while i < n:
fn()
i += 1
fn.times = times_
return fn

@iterative
def aFunction():
print "this is a function"

aFunction.times(3)

Prints:
this is a function
this is a function
this is a function

But this still seems like a lot of work to avoid "for x in range(n):".
-- Paul

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Zentrader
> Not to me.  If I read "for _ in ...", I wouldn't be quite sure what _ was.
> Is it some magic piece of syntax I've forgotten about?  Or something new
> added to language while I wasn't paying attention (I still consider most
> stuff added since 1.5 to be new-fangled :-)).

+1 to forgotten about
+1 to new-fangled=added since 1.5   When 3000 comes out I'll have to
break down and buy a new Python book.  Also, it's amazing how much
posting space is spent here replying to someone who is too lazy to key
in a variable name.  Damn!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Ivan Illarionov
Plain Python function are very often more powerful than classes:

>>> def go(count):
...   if not hasattr(go, 'count'):
... go.count = count
...   if go.count <= 0:
... del go.count
... return False
...   go.count -= 1
...   return True
...
>>> while go(3):
...   print 'hello'
...
hello
hello
hello
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Stef Mientki
be careful, "_"  is thé translation function used in Il8N, Il10N 
localization / internationalization
e.g.
 print  _( "hello" )

cheers,
Stef

[EMAIL PROTECTED] wrote:
> 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
>
> But I like using _ because it's only 1 character and communicates well
> the idea "I don't care about this variable."
>
> The only potential disadvantages I can see are threefold:
>
> 1. It might be a little jarring to people not used to it.  I do admit
> it looks pretty strange at first.
>
> 2. The variable _ has special meaning at the interactive interpreter
> prompt.  There may be some confusion because of this.
>
> 5.  Five is right out.  (ob Holy Grail reference, of course. :-)
>
> So, I guess I'm wondering if anyone else uses a similar idiom and if
> there are any downsides to it that I'm not aware of.
>
> Thanks
>
> Paul
>   

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Steve Holden
Jeff Schwab wrote:
> How [EMAIL PROTECTED] wrote:
>> 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
>>
>> But I like using _ because it's only 1 character and communicates well
>> the idea "I don't care about this variable."
>>
>> The only potential disadvantages I can see are threefold:
>>
>> 1. It might be a little jarring to people not used to it.  I do admit
>> it looks pretty strange at first.
>>
>> 2. The variable _ has special meaning at the interactive interpreter
>> prompt.  There may be some confusion because of this.
>>
>> 5.  Five is right out.  (ob Holy Grail reference, of course. :-)
>>
>> So, I guess I'm wondering if anyone else uses a similar idiom and if
>> there are any downsides to it that I'm not aw
> 
> Would something like this be acceptable?  It still requires a loop 
> variable, plus an extra line of code per loop, plus a one-time class 
> definition (and import into each client module), and it's probably 
> slower than "for dummy in range."  The syntax might be more inuitive 
> than "dummy" or "_" in a for loop, though.
> 
> class Go:
>  def __init__(self, count):
>  self.count = count
> 
>  def again(self):
>  if self.count <= 0:
>  return False
>  self.count -= 1
>  return True
> 
> go = Go(3)
> while go.again():
>  print "hello"

One could rename the again() method __call__(), allowing the even more 
horrible but less verbose

while Go(3)():
 print "hello"

That'd confuse the newbies!

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Steve Holden
Gabriel Genellina wrote:
[...]
>> On Feb 3, 2008 3:34 AM, Roy Smith <[EMAIL PROTECTED]> wrote:
>>
>>> But, more to the point, I'd try to find variable name which described  
>>> why I was looping, even if I didn't actually use the value in theloop  
>>> body:
> 
> Me too. Government don't collect taxes by the number of variable names  
> used (yet).
> 
And if they did I'd pay the tax to retain my sanity.

no-taxation-without-repr()-ly y'rs  - steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-03 Thread Robert Kern
James Matthews wrote:
> Because 0 is counted therefore i only have to do it 99 times

No, Gabriel is correct. range(n) creates a list of integers starting at 0 and 
going to n-1 (inclusive), not n.


In [1]: range(9)
Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8]

In [2]: len(range(9))
Out[2]: 9

In [3]: len(range(1, 9))
Out[3]: 8

In [4]: range(10)
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [5]: len(range(10))
Out[5]: 10


> On Feb 3, 2008 4:38 AM, Gabriel Genellina <[EMAIL PROTECTED] 
> > wrote:
> 
> En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews
> <[EMAIL PROTECTED] >
> escribió:
> 
> Sorry to be nitpicking, but people coming from other languages may get
> confused by the wrong examples:
> 
>  > What i do is a simple range call. for i in range(number of times
> i want
>  > to repeat something)
>  > I guess it comes from my C days for(i=0;i<100;i++) { or in python
> for i
>  > in range(99):
> 
> Should be `for i in range(100)` to match exactly the C loop. Both
> iterate
> 100 times, with i varying from 0 to 99 inclusive.
> 
>  >>  [EMAIL PROTECTED]  wrote:
>  >>
>  >> > 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
> 
> Should be `for _ in xrange(n)` to match the Ruby example. Both iterate n
> times.
> 
>  > On Feb 3, 2008 3:34 AM, Roy Smith <[EMAIL PROTECTED]
> > wrote:
>  >
>  >> But, more to the point, I'd try to find variable name which
> described
>  >> why I was looping, even if I didn't actually use the value in
> theloop
>  >> body:
> 
> Me too. Government don't collect taxes by the number of variable names
> used (yet).
> 
> --
> Gabriel Genellina

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Does anyone else use this little idiom?

2008-02-03 Thread James Matthews
Because 0 is counted therefore i only have to do it 99 times

Thanks

On Feb 3, 2008 4:38 AM, Gabriel Genellina <[EMAIL PROTECTED]> wrote:

> En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews <[EMAIL PROTECTED]>
> escribió:
>
> Sorry to be nitpicking, but people coming from other languages may get
> confused by the wrong examples:
>
> > What i do is a simple range call. for i in range(number of times i want
> > to repeat something)
> > I guess it comes from my C days for(i=0;i<100;i++) { or in python for i
> > in range(99):
>
> Should be `for i in range(100)` to match exactly the C loop. Both iterate
> 100 times, with i varying from 0 to 99 inclusive.
>
> >>  [EMAIL PROTECTED] wrote:
> >>
> >> > 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
>
> Should be `for _ in xrange(n)` to match the Ruby example. Both iterate n
> times.
>
> > On Feb 3, 2008 3:34 AM, Roy Smith <[EMAIL PROTECTED]> wrote:
> >
> >> But, more to the point, I'd try to find variable name which described
> >> why I was looping, even if I didn't actually use the value in theloop
> >> body:
>
> Me too. Government don't collect taxes by the number of variable names
> used (yet).
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://search.goldwatches.com/?Search=Movado+Watches
http://www.jewelerslounge.com
http://www.goldwatches.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Does anyone else use this little idiom?

2008-02-03 Thread Arnaud Delobelle
On Feb 3, 2:03 am, [EMAIL PROTECTED] wrote:
> 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
[...]

If 'some code' is a function (say f) you can write (with repeat from
itertools):

for action in repeat(f, n): action()

I don't know how 'Pythonic' this would be...

--
Arnaud

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-02 Thread Steven D'Aprano
On Sun, 03 Feb 2008 15:08:34 +1100, Ben Finney wrote:

>> But I like using _ because it's only 1 character and communicates well
>> the idea "I don't care about this variable."
> 
> Not to me. As you noted, '_' is easily ambiguous. Explicit is better
> than implicit; the name 'dummy' makes it much clearer.

Assuming you're not writing code like the following:

dummies = [Pacifier() for x in xrange(100)]
for dummy in dummies:
check_for_quality(dummy)


"dummy" as the name of a variable you don't care about is just a 
convention, no different from "_" or "paris_hilton" or "shrubbery". In 
fact, to me "dummy" implies that it holds a dummy value that will be  
replaced later with the actual value needed.

If you want an explicit name, try a variation of "dontcare". Assuming 
that you're an English speaker.

People seem to forget that "explicit" just means that there's a 
convention that nearly everybody knows, and if you follow it, nearly 
everybody will know what you mean. Often that convention is nothing more 
than the meanings of words in whatever human language you're speaking, 
but it's still a convention.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-02 Thread Steven D'Aprano
On Sat, 02 Feb 2008 18:03:54 -0800, miller.paul.w wrote:

> for _ in xrange (1,n):
>some code
... 
> So, I guess I'm wondering if anyone else uses a similar idiom and if
> there are any downsides to it that I'm not aware of.

Sometimes, but not often.

If I'm writing a use-once-then-throw-away script, it's too much trouble 
to press the Shift key to get an underscore *wink*, so I tend to just use 
i or x for the loop variable.

If I'm writing something I intend to keep, but don't care too much about, 
I'll use _, i or x about equal numbers of times, depending on whim.

If I was writing something important I expected others to read, I'd use 
dummy.

I wouldn't go so far as to say "Don't use that idiom!", but it's not 
something I use often.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-02 Thread Ben Finney
"Gabriel Genellina" <[EMAIL PROTECTED]> writes:

> Should be `for _ in xrange(n)` to match the Ruby example. Both
> iterate n times.

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


http://wiki.python.org/moin/Py3kDeprecated#head-343618ffa0887790ed12c3f9cf278cd7ca7eef51-2>

-- 
 \   "Prediction is very difficult, especially of the future." |
  `\   —Niels Bohr |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Does anyone else use this little idiom?

2008-02-02 Thread Ben Finney
[EMAIL PROTECTED] writes:

> 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
> 
> But I like using _ because it's only 1 character and communicates well
> the idea "I don't care about this variable."

Not to me. As you noted, '_' is easily ambiguous. Explicit is better
than implicit; the name 'dummy' makes it much clearer.

> The only potential disadvantages I can see are threefold:
> 
> 1. It might be a little jarring to people not used to it.  I do admit
> it looks pretty strange at first.
> 
> 2. The variable _ has special meaning at the interactive interpreter
> prompt.  There may be some confusion because of this.

The name '_' also has an established meaning for the "gettext"
internationalisation/localisation library, pre-dating Python and
documented in http://www.python.org/doc/lib/module-gettext>.

-- 
 \  "I have a large seashell collection, which I keep scattered on |
  `\the beaches all over the world. Maybe you've seen it." |
_o__)   —Steven Wright |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Does anyone else use this little idiom?

2008-02-02 Thread Jeff Schwab
How [EMAIL PROTECTED] wrote:
> 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
> 
> But I like using _ because it's only 1 character and communicates well
> the idea "I don't care about this variable."
> 
> The only potential disadvantages I can see are threefold:
> 
> 1. It might be a little jarring to people not used to it.  I do admit
> it looks pretty strange at first.
> 
> 2. The variable _ has special meaning at the interactive interpreter
> prompt.  There may be some confusion because of this.
> 
> 5.  Five is right out.  (ob Holy Grail reference, of course. :-)
> 
> So, I guess I'm wondering if anyone else uses a similar idiom and if
> there are any downsides to it that I'm not aw

Would something like this be acceptable?  It still requires a loop 
variable, plus an extra line of code per loop, plus a one-time class 
definition (and import into each client module), and it's probably 
slower than "for dummy in range."  The syntax might be more inuitive 
than "dummy" or "_" in a for loop, though.

class Go:
 def __init__(self, count):
 self.count = count

 def again(self):
 if self.count <= 0:
 return False
 self.count -= 1
 return True

go = Go(3)
while go.again():
 print "hello"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-02 Thread Gabriel Genellina
En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews <[EMAIL PROTECTED]>  
escribió:

Sorry to be nitpicking, but people coming from other languages may get  
confused by the wrong examples:

> What i do is a simple range call. for i in range(number of times i want  
> to repeat something)
> I guess it comes from my C days for(i=0;i<100;i++) { or in python for i  
> in range(99):

Should be `for i in range(100)` to match exactly the C loop. Both iterate  
100 times, with i varying from 0 to 99 inclusive.

>>  [EMAIL PROTECTED] wrote:
>>
>> > 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

Should be `for _ in xrange(n)` to match the Ruby example. Both iterate n  
times.

> On Feb 3, 2008 3:34 AM, Roy Smith <[EMAIL PROTECTED]> wrote:
>
>> But, more to the point, I'd try to find variable name which described  
>> why I was looping, even if I didn't actually use the value in theloop  
>> body:

Me too. Government don't collect taxes by the number of variable names  
used (yet).

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-02 Thread James Matthews
What i do is a simple range call. for i in range(number of times i want to
repeat something)

I guess it comes from my C days for(i=0;i<100;i++) { or in python for i in
range(99):

On Feb 3, 2008 3:34 AM, Roy Smith <[EMAIL PROTECTED]> wrote:

> In article
> <[EMAIL PROTECTED]>,
>  [EMAIL PROTECTED] wrote:
>
> > 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
> >
> > But I like using _ because it's only 1 character and communicates well
> > the idea "I don't care about this variable."
>
> Not to me.  If I read "for _ in ...", I wouldn't be quite sure what _ was.
> Is it some magic piece of syntax I've forgotten about?  Or something new
> added to language while I wasn't paying attention (I still consider most
> stuff added since 1.5 to be new-fangled :-)).  If I see "dummy", I know it
> means, "the language requires a variable here, but the value is not
> needed".
>
> > 1. It might be a little jarring to people not used to it.  I do admit
> > it looks pretty strange at first.
> >
> > 2. The variable _ has special meaning at the interactive interpreter
> > prompt.  There may be some confusion because of this.
>
> Wow, I didn't even know about #2.  Now you see what I mean about "some
> magic syntax"?  Surely, between, "It looks strange", and "there may be
> some
> confusion", that's enough reason not to use it?
>
> But, more to the point, I'd try to find variable name which described why
> I
> was looping, even if I didn't actually use the value in the loop body:
>
> for number_that_you_shall_count_to in xrange(3):
>   print "Whaaa"
>  --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://search.goldwatches.com/?Search=Movado+Watches
http://www.jewelerslounge.com
http://www.goldwatches.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Does anyone else use this little idiom?

2008-02-02 Thread Roy Smith
In article 
<[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

> 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
> 
> But I like using _ because it's only 1 character and communicates well
> the idea "I don't care about this variable."

Not to me.  If I read "for _ in ...", I wouldn't be quite sure what _ was.  
Is it some magic piece of syntax I've forgotten about?  Or something new 
added to language while I wasn't paying attention (I still consider most 
stuff added since 1.5 to be new-fangled :-)).  If I see "dummy", I know it 
means, "the language requires a variable here, but the value is not needed".

> 1. It might be a little jarring to people not used to it.  I do admit
> it looks pretty strange at first.
> 
> 2. The variable _ has special meaning at the interactive interpreter
> prompt.  There may be some confusion because of this.

Wow, I didn't even know about #2.  Now you see what I mean about "some 
magic syntax"?  Surely, between, "It looks strange", and "there may be some 
confusion", that's enough reason not to use it?

But, more to the point, I'd try to find variable name which described why I 
was looping, even if I didn't actually use the value in the loop body:

for number_that_you_shall_count_to in xrange(3):
   print "Whaaa"
-- 
http://mail.python.org/mailman/listinfo/python-list