Live data from Hacker News

I Challenge You to Debug These 7 Lines of Code Under 9 Minutes

theodo.fr

21–30 of 37 posts

Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes

#21
It took me a minute to see the problems, but - not having touched Python for over a decade - I can't for the life of me bring the lambda to capture the value of the variable.

I know this doesn't work (because python assignment rules)

    def foo(a, methods=[]): 
    m=[];
    for bla in methods:
        m.append(bla)

    for i in range(3): 
        k = i
        m.append(lambda x: x + k)


But this should work, according to quick'n'dirty google (because default assignment), and yet it doesn't.

    for i in range(3): 
        def bar(c, d=i): return c+d
        m.append(lambda x: bar(x))

EDIT: Ok, I'm something missing here.

This doesn't capture the value of i

    for i in range(3): 
        m.append(lambda x,i=i: x+i)
But this does. WTH?!

    for i in range(3): 
        m.append(lambda i=i: i)

Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes

#22
post #21

It took me a minute to see the problems, but - not having touched Python for over a decade - I can't for the life of me bring the lambda to capture the value of the variable. I know this doesn't work (because python assignment rules) def foo(a, methods=[]): m=[]; for bla in methods: m.append(bla) for i in range(3): k = i m.append(lambda x: x + k) But this should work, according to quick'n'dirty google (because defaul…

That's an interesting try! I think the default assignment is done by reference as well, so when i changes in the loop the default value changes as well? Similarly to methods=[]. Can't say for sure though. The solution I give in the article is the only one I could come up with, I'm interested if you find another one :)

Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes

#23

Took me less than 7 minutes with a single print statement. I'm a FE developer though, and python feels awfully similar to JavaScript.

Just one? I guess you found out about the methods default value this way, did you find the other one by eye?

Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes

#24

It's a common JavaScript interview question to ask about capturing variables in a closure. What really frustrates me with go is that it doesn't have a mature debugger so I have to resort to inserting print statements everywhere.

I give resources to debug JavaScript at the end of the article, did you miss them or are they not good enough?

Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes

#26

Great intro to PDB, but I have to be critical of: "The second group took 9 minutes on average, and the first one 14 minutes! Those are not completely statistically significant, but that’s still a 35% speedup on an 8-line-long program." If you want to talk about statistical significance, don't say "not completely". It either is or it isn't. If you think the p-value is important (and I'm not saying it is, but don't bot…

Article author here. I agree with your point, I shouldn't have been so cautious. I consistently observed people being faster when using debuggers and I wanted to give it a test on this problem. Most people don't see that they can get a significant speedup in using debuggers, so that sentence wanted to give a sense on how much quicker you can be. I'd be happy to hear of a better way to convince a non-debugger user tha…

Well I was convinced :) If it were me, I'd probably just remove the mention of significance -- or if you have more people and some time you can probably set up a proper experiment, control for some confounds and you'll probably get significant results.

Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes

#27
post #21

It took me a minute to see the problems, but - not having touched Python for over a decade - I can't for the life of me bring the lambda to capture the value of the variable. I know this doesn't work (because python assignment rules) def foo(a, methods=[]): m=[]; for bla in methods: m.append(bla) for i in range(3): k = i m.append(lambda x: x + k) But this should work, according to quick'n'dirty google (because defaul…

That's an interesting try! I think the default assignment is done by reference as well, so when i changes in the loop the default value changes as well? Similarly to methods=[]. Can't say for sure though. The solution I give in the article is the only one I could come up with, I'm interested if you find another one :)

Ah - the answer was so simple: I'm an idiot.

    for i in range(3): 
        m.append(lambda x,i=i: x+i)
actually works, I just had a brain fart. :)

Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes

#28

This is a common mistake in pretty much any language that captures a closure inside a lambda: C++, JS, etc. Unfortunately, debugging this the first time takes 20 minutes; the next time, it takes 0 seconds.

> common mistake in C++

Not really. You have to explicitly specify you want capture-by-reference with C++ lambdas.

The code would also look very out-of-place to a C++ programmer, because you would never capture a loop counter by reference if you intend to use the lambda outside of the loop. Lifetimes don't just get magically extended into completely unrelated scopes.

Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes

#30
The fact that "methods" is somehow kept defined in the scope of the function and preserved when you call it again and isn't always reinitialized to [] is just dumb. No wonder this was a bug, who the hell would see that and how is this "feature" ever useful except to create bugs?
Post reply on HN