I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
1–10 of 37 posts
Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#2Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#3Experienced in Python? This will take you less than one minute.
def foo(a, methods=[]): # methods defaults to [] if not specified
ohno.Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#4Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#5Re: I Challenge You to Debug These 7 Lines of Code Under 9 Minutes
#6With 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
#7Is 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.
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
#8Experienced in Python? This will take you less than one minute.
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
#9Is 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…
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
#10Earlier 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.