Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > what does 'for _ in range()' mean?

Reply
Thread Tools

what does 'for _ in range()' mean?

 
 
Dave Benjamin
Guest
Posts: n/a
 
      07-30-2004
In article <(E-Mail Removed)>, Peter Hansen wrote:
> Matteo Dell'Amico wrote:
>
>> Peter Hansen wrote:
>>
>>> Actually, not in the least, but I'm happy to go on faith that
>>> you have a point and hope you have managed to communicate it
>>> to others.

>>
>> Let's try it again: in functional programming languages, you can use
>> pattern-matching, so that you can define functions in a declarative
>> fashion this way:

>
> Oh! Enlightment dawns.... we were still talking about Ocaml then.
> I see.


Actually, this example more resembles Haskell than OCaml. In OCaml, you'd
typically write something more like this:

let f x =
match x with
| 1 -> 2
| 2 -> 3
| 3 -> 4
| _ -> 42

Or, as a shorthand

let f = function
| 1 -> 2
| 2 -> 3
| 3 -> 4
| _ -> 42

But the idea is the same, anyhow. "_" is special, and means something like
the "don't care" of digital logic.

In Python, you could also write this function with a dictionary:

def f(x):
return {
1: 2,
2: 3,
3: 4,
}.get(x, 42)

--
.:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.

"When the country is confused and in chaos, information scientists appear."
Librarian's Lao Tzu: http://www.geocities.com/onelibrarian.geo/lao_tzu.html
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      07-30-2004

Dave> Speaking of which, am I the only one here that sees this _()
Dave> function as a total hack?

It's a convention adopted by the i18n folks which got imported to Python.
Preexisting tools that wander through the source and build dictionaries of
string literals will work with C, Python or Perl (or whatever). The string

"My dog has fleas"

becomes

_("My dog has fleas")

That construct is a valid function call in many popular languages.

There's an extra side benefit as well. To internationalize code that
contains string literals you want to disturb code readability as little as
possible. _(...) seems to be the least visually obtrusive function call
available.

So, yes it's a hack, maybe even a total hack, but it's a hack with
history. <wink>

Skip
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      07-30-2004
In article <(E-Mail Removed)>,
Skip Montanaro <(E-Mail Removed)> wrote:

> Dave> Speaking of which, am I the only one here that sees this _()
> Dave> function as a total hack?
>
> It's a convention adopted by the i18n folks which got imported to Python.
> Preexisting tools that wander through the source and build dictionaries of
> string literals will work with C, Python or Perl (or whatever). The string
>
> "My dog has fleas"
>
> becomes
>
> _("My dog has fleas")


It should become "My dog has no nose".

I wonder how those tools deal with all the interesting sorts of ways
Python has for quoting strings?
 
Reply With Quote
 
Heike C. Zimmerer
Guest
Posts: n/a
 
      07-30-2004
Dave Benjamin <(E-Mail Removed)> writes:

> In article <(E-Mail Removed)>, Heike C. Zimmerer wrote:
>> Dave Benjamin <(E-Mail Removed)> writes:
>>
>>> In Python, it's possible to use _ multiple times in the same expression,

>>
>> There is, however, one objection to the use of _ as a placeholder for
>> a dummy variable: it is used by the gettext module. So if a program
>> may be subject to internationalization, it is advisable to keep this
>> name reserved, or you may run into problems.

>
> Speaking of which, am I the only one here that sees this _() function as a
> total hack? Usually, having a one-character function (and a global one at
> that!) would be considered outrageous,


I agree to some degree. It's convention, however, and there are
useful tools supporting it.

> and why not have global string
> constants instead of looking up complete sentences as keys in a mapping
> table?


This would mean introducing another level of indirection. Having the
entire english message inside the program text right where it is used
saves you the need to look it up elsewhere or to guess from the name
what it is supposed to print out. It's easy to change the text if it
changes its meaning. You'd have to change both the text and its
descriptive name otherwise.

And the additional effort of looking up the entire text shouldn't be
much of concern; it's usually within an I/O-bound operation.

> Not that I could propose a better solution if you're trying to make
> an already existing, large application multilingual, but still...


Yes. Not that I couldn't think of a better solution, but for me, it
serves its purpose and it works well.

- Heike
 
Reply With Quote
 
Dave Benjamin
Guest
Posts: n/a
 
      07-30-2004
In article <(E-Mail Removed)>, Heike C. Zimmerer wrote:
> Dave Benjamin <(E-Mail Removed)> writes:
>
>> In article <(E-Mail Removed)>, Heike C. Zimmerer wrote:
>>> Dave Benjamin <(E-Mail Removed)> writes:
>>>
>>>> In Python, it's possible to use _ multiple times in the same expression,
>>>
>>> There is, however, one objection to the use of _ as a placeholder for
>>> a dummy variable: it is used by the gettext module. So if a program
>>> may be subject to internationalization, it is advisable to keep this
>>> name reserved, or you may run into problems.

>>
>> Speaking of which, am I the only one here that sees this _() function as a
>> total hack? Usually, having a one-character function (and a global one at
>> that!) would be considered outrageous,

>
> I agree to some degree. It's convention, however, and there are
> useful tools supporting it.
>
>> and why not have global string
>> constants instead of looking up complete sentences as keys in a mapping
>> table?

>
> This would mean introducing another level of indirection. Having the
> entire english message inside the program text right where it is used
> saves you the need to look it up elsewhere or to guess from the name
> what it is supposed to print out. It's easy to change the text if it
> changes its meaning. You'd have to change both the text and its
> descriptive name otherwise.
>
> And the additional effort of looking up the entire text shouldn't be
> much of concern; it's usually within an I/O-bound operation.
>
>> Not that I could propose a better solution if you're trying to make
>> an already existing, large application multilingual, but still...

>
> Yes. Not that I couldn't think of a better solution, but for me, it
> serves its purpose and it works well.


All very good points. Thanks.

--
.:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.

"When the country is confused and in chaos, information scientists appear."
Librarian's Lao Tzu: http://www.geocities.com/onelibrarian.geo/lao_tzu.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET 2.0 ASPx Page does not load, but HTM does prabhupr@hotmail.com ASP .Net 1 02-08-2006 12:57 PM
Button OnClick does not fire on first postback, but does on second Janet Collins ASP .Net 0 01-13-2006 10:08 PM
Does the 2.0 Framework come out when Visual Studio .NET 2005 does? needin4mation@gmail.com ASP .Net 3 10-07-2005 12:55 AM
CS0234 Global does not exist ... but it genuinely does Bill Johnson ASP .Net 0 07-08-2005 06:34 PM
Does no one else think microsoft does a poor job? =?Utf-8?B?SmVyZW15IEx1bmRncmVu?= Wireless Networking 2 11-20-2004 12:17 AM



Advertisments