Live data from Hacker News

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

theodo.fr

31–37 of 37 posts

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

#31

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?

It's absolutely insane that this is apparently the expected behavior. It makes no sense whatsoever except to give people who know this an ego boost when they can successfully work around Python's unintuitive semantics while inexperienced people cannot. It completely breaks functions, the most fundamental unit of encapsulation and abstraction in every modern programming language.

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

#32

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?

[deleted]

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

#33

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 the "capturing i in a closure by reference" part, since that error is also common in other languages which capture variables by reference, and don't create a new reference per loop iteration. However the default parameter issue looks like a python specific issue which is really strangely designed. I would normally say [] is a literal for an empty list which creates a new instance of an empty list every t…

>Is [] is global reference to a single mutable list which is only initially empty?

No, the problem is default arguments to methods are evaluated once, when the method is created and that value is used for all calls. I don't know why that is, its definitely strange - any other language would evaluate the default args each time a function is called just like the regular args.

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

#34
It's mystifying to me that the author comes to the conclusion that the solution to this is to educate people on debugging tools and edge-case semantics. The reason the code is difficult to debug is because the "bug" is that functions are apparently completely broken and violate every reasonable assumption anybody is likely to make about them without a priori knowledge about this specific edge case. Why isn't the conclusion then, that Python is broken, and has a serious and fundamental problem? Why doesn't anybody see this as a "broken window", that leads us as a culture to believe that this sort of thing is acceptable and to be expected and not an ugly, horrible, and utterly unnecessary problem? Is this really the language we're supposed to teach to beginners? How many combined hours have been wasted on solving problems generated by this behavior, and for what purpose? I don't understand why people accept this.

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

#35

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?

It's absolutely insane that this is apparently the expected behavior. It makes no sense whatsoever except to give people who know this an ego boost when they can successfully work around Python's unintuitive semantics while inexperienced people cannot. It completely breaks functions, the most fundamental unit of encapsulation and abstraction in every modern programming language.

It's surprising how few languages actually support anonymous functions with closures properly.

Not to plug my 2 favorites that do (Ruby and Elixir/Erlang) or anything...

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

#36

Earlier quoted context omitted.

I understand the "capturing i in a closure by reference" part, since that error is also common in other languages which capture variables by reference, and don't create a new reference per loop iteration. However the default parameter issue looks like a python specific issue which is really strangely designed. I would normally say [] is a literal for an empty list which creates a new instance of an empty list every t…

>Is [] is global reference to a single mutable list which is only initially empty? No, the problem is default arguments to methods are evaluated once, when the method is created and that value is used for all calls. I don't know why that is, its definitely strange - any other language would evaluate the default args each time a function is called just like the regular args.

Then you would have the problem that something like mydefault=3, def f(default=mydefault):... would change it's evaluation if anyone ever defined mydefault=4. That would leave users unable to define any variable thats ever mentioned in default values of any function in any library they are using without causing very unexpected behaviors.

What languages are you thinking of where default values set to a references are set to a copy of that reference as it was at definition time at every evaluation?

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

#37
post #36

Earlier quoted context omitted.

>Is [] is global reference to a single mutable list which is only initially empty? No, the problem is default arguments to methods are evaluated once, when the method is created and that value is used for all calls. I don't know why that is, its definitely strange - any other language would evaluate the default args each time a function is called just like the regular args.

Then you would have the problem that something like mydefault=3, def f(default=mydefault):... would change it's evaluation if anyone ever defined mydefault=4. That would leave users unable to define any variable thats ever mentioned in default values of any function in any library they are using without causing very unexpected behaviors. What languages are you thinking of where default values set to a references are…

Not sure if i completely understand, but the example you mentioned would work in Scala. If a mutable variable is referenced in default args, its value at define-time is used.

Example:

    scala> var x = 5
    x: Int = 5

    scala> def func(arg1: Int = (x+1)) = { println(arg1)}
    func: (arg1: Int)Unit

    scala> func()
    6

    scala> var x = 7
    x: Int = 7

    scala> func()
    6 //unchanged
For reference vars the closure captures a local copy:

    scala> var list = List(1,2,3)
    list: List[Int] = List(1, 2, 3)

    scala> def printList(arg1: List[Int] = lis) = { println(arg1) }
    printList: (arg1: List[Int])Unit

    scala> printList()
    List(1, 2, 3)

    scala> var list = List(4,5,6)
    list: List[Int] = List(4, 5, 6)

    scala> printList()
    List(1, 2, 3)
Post reply on HN