4 ms·
Well, I guess I just failed your interview. As that's perfectly valid Python that does exactly what I expect, what's "wrong" with it?
by dTal 10y ago
Well, I guess I just failed your interview. As that's perfectly valid Python that does exactly what I expect, what's "wrong" with it?
- falcolas 10y agoRun `foo('a')` a few times in a row - you'll start seeing that "bar" keep state unexpectedly. It's a fairly classic Python gotcha - the mutable object is retained across calls when the default is used. >>> def foo(a, bar=[]): ... bar.append(a) ... return bar ... >>> foo('a') ['a'] >>> foo('a') ['a', 'a'] >>> foo('a') ['a', 'a', 'a'] >>> foo('a') ['a', 'a', 'a', 'a'] EDIT: Having the program output as part of the problem would probably help. :D
- dTal 10y agoWhat. The. Ass. Okay, minus another couple points to me for not testing each case more than once, and yes, if you're going to present syntactically valid code and ask people to "fix" it you should probably state what it's supposed to do, but aside from all that... the hell? Okay, having Googled the logic behind this behaviour, I still think it's stupid. My next question is - is being aware of little language gotchas like this really so important? It really bugs me that about all I'm good for in the employment market is making computers do stuff, but I'll never pass an "esoteric language behaviour test" in anything - my tastes are too diverse and I've never gone "all-in" on anything. But I don't think it gets in the way too much - you keep your changes modular and test frequently, and if it doesn't work right you figure out why. Which corners of the language you tend to bump into depends very much on your coding style (for instance I try hard not to mutate things which is possibly why I've not hit this before), so even with a brand new language you should be able to get going pretty quickly. Surely I'm not useless?
- falcolas 10y ago> What. The. Ass. Yeah. Python. A fun little wart. > is being aware of little language gotchas like this really so important Somewhat ironically, our conversation here highlights why it is important. If you initially tested these three lines of code to your satisfaction and didn't find the bug, how could you assume that your unit tests would catch the bug? > Surely I'm not useless? I would hardly think you're useless, but it does show a lack of experience with the Python language. On the other hand, your most recent response also shows a capacity for looking up and identifying the language WTF and how to fix it, skills which are important in lieu of experience.
- flukus 10y ago> Somewhat ironically, our conversation here highlights why it is important. If you initially tested these three lines of code to your satisfaction and didn't find the bug, how could you assume that your unit tests would catch the bug? If you can't define the behavior for such a simple function how are they supposed to know if it's working or not? Several posts later you still haven't. Is foo meant to append to the array? Is it meant to initialize a new one? Is it meant to create one if it's null?
- dTal 10y agoEh, I'll give falcolas a pass on that, since the intended behaviour is clearly what any normal person looking at that would think it does - if you wanted to make some sort of closure, I'd hope you'd do it more explicitly (though my opinion of good style and what's supposedly "Pythonic" are often at odds - the whole "[].append()" idiom seems perverse to me). It's not necessarily unreasonable to present technically valid code and say "find the bug" - in C for example you might see "if(x=3)" and conclude, on the basis of what's idiomatic, that it's probably an error.
- flukus 10y ago> since the intended behaviour is clearly what any normal person looking at that would think it does The function is called foo, any normal person would say "wtf is that supposed to do?".
- andrewstuart 10y agoTo me this is not a good test. It is a Python gotcha, and nice if you recognise it, but this definitely does NOT give you any indication of whether or not this person is a good programmer. What can be said is that if someone recognises this isssue then likely they are a good Python programmer. But the converse is not true that all good Python programmers would recognise this, and therefore this is a test that potentially eliminates a bunch of good programmers. That's a bad test. This test allows the interviewer to feel smug about how much smarter they are than the interviewee. LOTS of interview tests are set up that way. "Oh, you don't know some very specific thing about your primary programming language? Then (jeering) you're not very good are you!! I know that and you don't!". That's not skills evaluation, it's ego wrapped in the guise of skills evaluation. This is "trick oriented recruiting" and not far removed to me from asking how many ping pong balls would fit in a 747.