Live data from Hacker News

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

theodo.fr

1–10 of 37 posts

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

#5
post #2

Experienced in Python? This will take you less than one minute.

The first line tipped me off :p def foo(a, methods=[]): # methods defaults to [] if not specified ohno.

I got that one, but the rest confused me - I need to work with lambdas more!

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

#6
Is this a designed feature or something that just happened? I really think that this behaviour is erroneous and unexpected.

With python (and many other languages) I find myself getting bitten by behaviour like this every now and then. I use scheme for almost all my personal projects, and I can only remember getting bitten twice. Once was unexpected, and the other was laziness on my part.

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

#7

Is this a designed feature or something that just happened? I really think that this behaviour is erroneous and unexpected. With python (and many other languages) I find myself getting bitten by behaviour like this every now and then. I use scheme for almost all my personal projects, and I can only remember getting bitten twice. Once was unexpected, and the other was laziness on my part.

Basically it just boils down to reference vs value. Both of the errors in this code occur because python uses a reference instead of a value – I agree that they can be annoying and somewhat unexpected, but they make sense once you realize what's behind them.

In the method=[] part, the error occurs because python creates a single list, and passes that list by reference (as all lists are passed) into the function.

In the lambda part, the error occurs because python captures the variable from the enclosing scope by reference, not by value (as normal functions do with globals).

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

#8
post #2

Experienced in Python? This will take you less than one minute.

I don't even know Python, but after looking at it for a couple of minutes, I had a sense:

1. mutating the `methods` parameter directly, I don't even care if it works, is a bad idea

2. naming the thing methods and method might trip something up

By just changing the code so that we declare the local methods array (name it methodsX or something else) to be empty, this fixed the first 3 problems (where the function is empty). I don't know enough of Python syntax to make it accept the second parameter being a array of functions.

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

#9

Is this a designed feature or something that just happened? I really think that this behaviour is erroneous and unexpected. With python (and many other languages) I find myself getting bitten by behaviour like this every now and then. I use scheme for almost all my personal projects, and I can only remember getting bitten twice. Once was unexpected, and the other was laziness on my part.

Basically it just boils down to reference vs value. Both of the errors in this code occur because python uses a reference instead of a value – I agree that they can be annoying and somewhat unexpected, but they make sense once you realize what's behind them. In the method=[] part, the error occurs because python creates a single list, and passes that list by reference (as all lists are passed) into the function. In t…

I understand how it is sort of a detail of how python works. But I also can't understand why someone thought "hmm. this is rather unexpected. Let's keep it that way". It would be trivial to have python re-create an empty list.

Or is this pattern actually used? It seems rather perl5-y.

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

#10

Earlier quoted context omitted.

Basically it just boils down to reference vs value. Both of the errors in this code occur because python uses a reference instead of a value – I agree that they can be annoying and somewhat unexpected, but they make sense once you realize what's behind them. In the method=[] part, the error occurs because python creates a single list, and passes that list by reference (as all lists are passed) into the function. In t…

I understand how it is sort of a detail of how python works. But I also can't understand why someone thought "hmm. this is rather unexpected. Let's keep it that way". It would be trivial to have python re-create an empty list. Or is this pattern actually used? It seems rather perl5-y.

For empty lists it would be trivial, yes, but for arbitrary objects it wouldn't be. And only the empty list behaving different would be weird.
Post reply on HN