top | item 1897330

The Python IAQ: Infrequently Answered Questions

83 points| albertcardona | 15 years ago |norvig.com | reply

36 comments

order
[+] KirinDave|15 years ago|reply
I dunno why people think functional programming jobs are vanishingly scarce. They are not. If you can demonstrate skill in functional languages, you might even get recruited.

It's true there are more wage-slave code-a-day jobs, but those jobs can be easily filled by uninspired hacks who are in it for the money. If you're reading this, the odds of you being one of those hacks is very low. The job market is smaller, but the workforce of talented functional programmers is much smaller, so the demand is still high.

And Clojure & Scala are even easier because you can get a java job and them worm them in. It's not difficult to make a compelling argument that Clojure & Scala are better at Java than Java is.

[+] gaius|15 years ago|reply
Functional programming jobs are scarce, but that's because no-one is a "functional programmer" in the same way that you might be a "PHP programmer". Instead you get lots of jobs where you happen to write a lot of code in a functional programming language but you don't look like a programmer in an organizational sense - you're an "analyst" or an "engineer" or whatever. That's where F# is making inroads.
[+] pilom|15 years ago|reply
Great article, bad title. Someone with more kharma mind changing it to Python Infrequently answered Questions?
[+] TomasSedovic|15 years ago|reply
I agree.

The article actually contains the "if u cn rd ths..." phrase and it's a nice touch. However, it would be better to keep the original title and mention the phrase in the comments.

[+] shabda|15 years ago|reply
I don't get it anyway, if I can read this, why can I get a job in functional programming?
[+] JustinSeriously|15 years ago|reply
It was a weird title. I did an an quick poll of two non-programmer, LOL-speak experts, and I got these answers:

- If you can read this, you can get a job in financial program (if there were any)

- If you can read this, you can get a job in functional programs/programming (if there were any)

I don't know what this proves.

[+] danio|15 years ago|reply
Is there much benefit to

    fact = lambda n: _if (n <= 1) (1) (lambda: n * fact(n-1))
over

    def fact(n):
        if (n <= 1):
            return 1
        else:
            return n * fact(n-1)
1 line vs 5 lines doesn't seem like an advantage to me when it takes pretty much the same amount of time to read and understand the complicated 1 liner.

Is there more optimisation possible?

[+] Goladus|15 years ago|reply

     python -c "import _if; fact = lambda n: _if (n <= 1) (1) (lambda: n * fact(n-1)) ; fact(5)"
The main advantage of a 1-liner is that you can compose the whole thing and execute it without having to format anything. That is a nice feature for code that is only ever intended to be used once, such as from the python repl or some other shell. --Or more than once, for the duration of a session. It's a lot easier to use command history to get and modify a 1-liner than to replay a 5-line function definition.

When the code is primarily expressions, it's relatively easy to to this. Trying to do one-liners with regular python syntax can get tricky.

Also, if you have a series of similar relationships to present, it's often easier to line them up next to each other. You might cut 12 lines to 6 lines and the whole thing will be far clearer. Although in those cases, it's probably more effective to use a dictionary to achieve the same thing, with the advantage of data/logic separation.

[+] Kilimanjaro|15 years ago|reply
Sometimes I put short ifs in one line, just personal taste

    def fact(n):
        if n<=1: return 1
        else:    return n * fact(n-1)
[+] gaius|15 years ago|reply
They are of broadly equivalent complexity (if you are used to Python) and you can fit more of the former in an editor window.

I don't like tho' that "lambda" is a keyword in Python, I much prefer Haskell's \ or OCaml's fun.

[+] d0mine|15 years ago|reply

   from math import factorial
Or

   fact = lambda n: reduce(operator.mul, xrange(1, n+1), 1)
[+] anateus|15 years ago|reply
Some of the responses are outdated, here's one of the things that is now part of python:

Python 2.5 introduced defaultdict in the collections module:

   from collections import defaultdict
   d = defaultdict(int)
   d['foo'] +=1 # look ma! no error!
For this application, if you're using Python 2.7 there's now a Counter in collections, you can do the same as above, just call Counter(). You can also do:

   c = Counter(iter) # iter is an iterable. It will count the elements!
[+] noahlt|15 years ago|reply
It's also worth noting that, in Python 2.6 and higher, we DO have a ternary operator:

    x = 3 if foo() else 6
[+] tomotomo|15 years ago|reply
If u cn rd ths... then you may speak Hebrew or Arabic, which are basically written without vowels.
[+] albertcardona|15 years ago|reply
Regarding the "if thr wr any", and the rise of Haskell and Clojure, I wonder whether such caveat applies any longer.

As for content of the Python IAQ, I've enjoyed learning about two python idioms I had never known possible:

1. The ternary conditional operator:

x = <result> if <test> else <alternative>

2. Using or to assign an alternative value when the first is False or None

x = <option> or <option>

[+] TallGuyShort|15 years ago|reply
Incidentally, most C-style-syntax languages have a ternary conditional operator, and I use it quite a lot for things like building strings, very small conditionals, etc.... When used correctly, it can improve readability and expressiveness quite a bit.

x = <test> ? <result> : <alternative>

Some times for slightly more complex expressions, I'll put them on separate lines so as to have a very compact if/else structure.

[+] adambyrtek|15 years ago|reply
It's worth noting that Peter himself mentions that most of the ternary tricks he describes are not very readable and wouldn't be considered Pythonic.

   Beautiful is better than ugly.
   Explicit is better than implicit.
   Simple is better than complex.
Personally I'd hate to see something like that in any real-world project.

   (test and [result] or [alternative])[0]
[+] IgorPartola|15 years ago|reply
As pointed out in the OP, there are actually quite a lot of ways to simulate the ternary operator in Python. An interesting one is (result, alternative)[test]. If result and alternative are functions you could write it as (result, alternative)[test]().

And for desert: I love this syntax:

  if 0 < x <= 100: pass
For more info see: http://stackoverflow.com/questions/394809/python-ternary-ope...
[+] reinhardt|15 years ago|reply
"..and the rise of Haskell and Clojure.."

Is there any real world evidence of said claim, outside geek-fetish bubbles like HN ?