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

D

Dave Benjamin

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)
 
S

Skip Montanaro

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
 
R

Roy Smith

Skip Montanaro said:
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?
 
H

Heike C. Zimmerer

Dave Benjamin said:
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
 
D

Dave Benjamin

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


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.


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.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top