tags:

views:

589

answers:

15

What questions do you think should a good Python programmer be able to respond to?

PS: I have seen this question based on other languages but not Python!

+5  A: 

How can you use a mutable object as a default value to a function?

And the closely related...

What are possible consequences of doing this?

def append_to( b, c=[] ):
    c.append( b )
S.Lott
Not every c has to have append() implemented?
piobyz
@piobyz: that's a good duck-typing part of the answer. But it's not the proper default value answer.
S.Lott
+5  A: 

Some fancier language features:

  • What are generators, iterators? (bonus points for views)
  • What are "new-style" classes and how do they differ from old-style classes?
  • What are packages, how do you make one?
  • What are decorators?

I'd say anyone with some experience has used one of these:

  • Where can you find a deque, priority queue, binheap implementation?
  • Have you worked with Twisted, SciPy, NumPy, GTK/Qt/Wx?
  • What is PyPi, what is easy_install ?
THC4k
Shouldn't the dictionary view be a separate question from generator and iterator? It feels different to me; maybe because it's a Python 3 thing.
S.Lott
Don't forget descriptors and metaclasses
gnibbler
+3  A: 

Why does this happen? Why isn't the answer -1?

>>> 3.1-4.1
-0.99999999999999956
S.Lott
what's the answers ?
Antoine Claval
In this vein: what is the result of 1000000^2? How is the type of the result different from the type of 1000000? Since we're discussing the internal implementation details of numbers...
Mike DeSimone
That's a nice feature of Python (not hiding floating point representation issues) but not strictly Python related
Francesco
This may be a common thing in almost all languages, but it is asked about Python with amazing frequency. I think this is the most frequently asked Python question on SO.
S.Lott
@Mike DeSimone: Please post that as a separate question so I can vote it up.
S.Lott
Mike, the result of 1000000^2 is 1000002. Both are ints. Careful how you phrase that question.
Adam Crossland
I usually point people to "What Every Computer Scientist Should Know About Floating-Point Arithmetic" by David Goldberg, Computing Surveys, 1991 (http://docs.sun.com/source/806-3568/ncg_goldberg.html).
Bill Karwin
@Adam: Ah, but one is an int and one a long in Python 2.x, right? My whole point was that these are trick questions, where you look like you're giving enough information but you really are not. It's like the grade school question of "What is 2 and 2?" There are two answers: 22 and 4, so the questioner just cites the other as correct. Heads, I win, tails, you lose. (For the record, I answered 2. I've always been a tech geek.) @S.Lott: After having said that, posting this as an answer so it can be upvoted would be hypocritical.
Mike DeSimone
Mine was not a trick question. Mine was plain old "understand this" for a common feature of Python floating-point numbers.
S.Lott
+8  A: 
  1. how do you sort a list of dicts by the value each has for key 'foo'?
  2. how do you get the highest 10 integers out of a list of a million integers?
  3. how do you sort a list of strings in case-insensitive alphabetical order?
  4. given a list of ints, how do you make a string with their str forms separated by spaces?
Alex Martelli
Does a good Python programmer need to have `sort()`'s semantics memorized, or is looking it up on docs.python.org allowed?
Mike DeSimone
No, you must have them tattoed to your forearm Mike :)
Skurmedel
@Mike DeSimone: Sort a list of dicts is a very frequently-asked question, and the answer has little to do with sort and everything to do with making proper use of the built-in types.
S.Lott
Answer to 4: `repr(list_of_ints)[1:-1].replace(",","")` :)
gnibbler
@gnibbler Alternatively, `" ".join([str(x) for x in ints])`
Eric
@Eric: generator would suffice
SilentGhost
@Eric: or `" ".join(map(str,list_of_ints))`. Using repr turns out to be faster for lists longer than about 10 integers though...
gnibbler
@S.Lott: I'd answer that (for 1), you would set `sort()`'s comparison function to `lambda x, y: cmp(x['foo'], y['foo'])`, but I don't have `sort()`'s argument list memorized (IRL, I'd just check docs.python.org). Do I fail the test?
Mike DeSimone
@Mike, so do check the docs and see whether it makes any sense to set the comparison function (instead of the key function). When I'm interviewing a candidate that claims to be "a good Python programmer" (by "good" let's say a self-rating of 7 or 8 on a 1 to 10 scale; 9 or 10 would be "excellent"), a couple failed questions like this tell me the self-rating is bogus/inflated -- which I imagine is the point of this question.
Alex Martelli
@Alex: And when I check the docs, I'll see the `key` parameter (which I forgot about because I don't use `sort()` nearly as often as I create a mapping `dict`) and use it. I'm not going to let pride get in the way of doing it right. You get a 90 in school, hey great, that's an A... You get 90% right in real life, you better hurry up and fix that 10%! ^_^ They're good questions; I was just wondering why 3 of the 4 revolved around using `sort()`.
Mike DeSimone
Trick comment @Mike: #2 does **not** "revolve around using `sort`" (sorting a million-items list instead of using `heapq.nlargest` would be an immediate indication that a candidate is just not familiar enough with Python's powerful standard library to be a **good** Python developer at this time -- so such a claim in their resume would be invalidated... it's easy stuff to learn, but if you haven't learned it you're not "good" at Python -- _yet_ -- and "to know yourself" is the most important of all knowledge;-).
Alex Martelli
Now that I look at it again, I realize that I would have answered #2 without using `sort()` or `heapq`; I would have used a `set` and another variable to cache the tenth largest value. Still sounds like it would cost me the job by showing me to be mediocre when I thought I was good. Thanks for the insight.
Mike DeSimone
@Mike, I guess you can always recode bits and pieces that are present, debugged and optimized in the standard library (not as robust, not as fast, etc;-), but I think we agree that this is not optimal. The standard library is large, but some bits and pieces (such as heapq -- though, even more, modules such as itertools and collections) come up so often in practical use that they're WELL worth learning (and reusing;-).
Alex Martelli
My problem is that I learned Python 1.5.6/2.0 (back when it had a huge file for choosing what modules to build... I don't miss that, or Tkinter) and I haven't kept up with everything added after that. I never got on a "What's New in Python" mail list. ^_^; When a ratchet wrench has worked well enough for you, you don't always think of going out and looking for an impact wrench.
Mike DeSimone
@Mike, a resume claim of "good with Python 1.5.2" is perfectly reasonable, just like saying that one is good at Old English (I wish I was good at that, so I could enjoy Beowulf more!); but a claim without classification, such as "good at Python" or "at English", is quite normally taken to imply the _current_ version of the language, so the explicitness is necessary IMHO (I do ask "what version" when a candidate produces perfectly good Python 1.5.2 _code_ -- I think my Java-interviewing colleagues aren't quite that lenient -- but "questions", as per this Q, doesn't lend itself to that).
Alex Martelli
+3  A: 

What is the difference between str and unicode, and which would you use in what circumstances?

Ranieri
`str` exists in the present, while `unicode` exists in the past. I'd use `str` unless I happened to need to maintain legacy code. :-)
Ken
+2  A: 

This is the most often heard question (at least to me)

Why would I use python if I already know xxxx (insert any language here) ?

I know, not as impressive as the other answers....

Kristian Damian
And the answer is...?
Ken
almost always a flame war
Kristian Damian
+9  A: 

Why should you refer to "names" and "binding" in Python instead of "variables" and "assignment"?

Peter Hansen
Feel free to answer this question please, as I have been writing python for years and don't know the answer.
Graeme Perrow
Well, in Python, you do not put a value in a variable, you bind (label) a value to a label. The distinction is not that useful, since with knowledge of references in other languages and some try / fail / redo using mutable default values for functions arguments, you get the most of it.
e-satis
@Graeme, pages like http://effbot.org/zone/python-objects.htm and http://www.python.net/crew/mwh/hacks/objectthink.html or http://www.win.tue.nl/~wstomv/edu/python/python-observations.html#NameObjectValue answer it reasonably well. My answer to my above question would be "because if you don't understand why you should, your non-Python mind-frame will lead you to make mistakes in a half dozen key areas (and some corner cases) of Python".
Peter Hansen
+2  A: 

What kinds of application would Python not be good for?

John
What is it? Even I don't know. I thought it's good for every kind of application.
jpartogi
Well, embeded devices where there is a good Java implementation, apps where advanced C# is needed and IronPython can't do it, app with performance issues or big mathematical calculations you don't want to split between Python and C, application with heavy multithreadings (supercalculators), apps that must play with PHP libs, apps you want to be maintainable by more than 10 % of the dev in the world, client side web app, etc, etc.
e-satis
"big mathematical calculations" => NumPy or SciPy.
Alex
@Alex... are those Python or Python wrappers around something fast? If the latter, that's a perfect example!
John
The latter, though still not as fast as C. But much faster than regular python.
Alex
I don't know Python well enough to _answer_ my question. The point is a Python developer should be able to discuss issues such as performance, and defend the use of Python if challenged with "it's too slow for maths".
John
Heh. I remember when people said *Fortran* was the only acceptable language when it came to math performance.
Ken
I wouldn't use it to control the avionics of a fighter jet. I also wouldn't use it to control a nuclear power plant. I wouldn't use it for medical devices either.
Bryan Oakley
@Bryan "... and why?" would be the follow up question.
John
+1  A: 
  • What do you get when you do "hello " * n where n is a number.
  • What does 10 ** 2 do?
  • How do you unpack a tuple?
Skurmedel
+4  A: 

What's a Python decorator and how is it used?

wheaties
Oh, was going to suggest this. +1
Skurmedel
Well, I just picked it up this year and lived really well before without understanding it.
e-satis
+6  A: 

What is duck typing ?

Ahmed Kotb
I'll vote this one because it really confuses me. My head is unable to wrap around this concept, i wish i can make this a question of it own. Any explanation on this?
gath
"if it walks like a duck, looks like a duck, acts like a duck, then it's a duck" :Dduck typing allows polymorphism without inheritance , so object just have to contain the method it is expected to implement , you will find in the wikipedia page a python example where two classes (person and duck) have the same method name. so person can be used to imitates a duck. there are also a lot of questions explaining duck typing in StackOverFlow.http://stackoverflow.com/search?q=duck+typinghope this comments helps.
Ahmed Kotb
what else floats ? a duck!
Stefano Borini
+3  A: 

What is list comprehension? What is 'lambda' used for?

Alexander Gessler
+4  A: 

I have a list of lists:

l=[[0]]*10
print(l)
# [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]

Why is it that when I change one element, all the elements change:

l[0][0]=1
print(l)
# [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]
unutbu
+3  A: 

What is going to print :

def paradox():
    try:
        raise Exception("Here")
    except:
        return "There"
    finally:
        return "Or maybe there"

    return "Or it that here?"

print paradox()

I love this one, because even the best programmers idle before answering.

e-satis
And the next question is: What do you think we do with someone who writes code like this?
Johan
And the next question is: What do you think should be done with someone who builds such a *feature* into a programming language?
just somebody
I must be a bad programmer. I got it right away. Plus, I write bad code. :-)
Ken
A: 

Why is mutability and immutability important? When ought you to care? (Note: The author of this question is still quite fuzzy on these.)

While much is "batteries included," what are the three most esoteric add-ons to Python you have used?

What is your general strategy for researching and performing Windows functions in Python?

Tell me about your favorite Python idiom. How did you discover it? How did you refine it?

What are some of the shortcomings in the development of Python and the surrounding ecosystem?

MetaHyperBolic