what does 'for _ in range()' mean?
Dave Benjamin
ramen at lackingtalent.com
Fri Jul 30 12:49:17 EDT 2004
In article <-IednYvL85_frJTc4p2dnA at powergate.ca>, 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
More information about the Python-list
mailing list