views:

157

answers:

3

This is a strange request but I'm looking for buggy Python code. I want to learn more about bugs and debuggers and I need some buggy code to work with. Unfortunately, all the code I've written is short and bug-free (so far).

Preferably it's not GUI stuff (b/c I'm just starting to learn it) but anything's good.

Thanks in advance

+4  A: 

Not sure how to scout "intentionally" for source code with bugs but you can look into the bug trackers of the main Python projects (and the less widespread ones, too), look for the bugs the reports refer to and debug them. It's a win-win situation. You win the skill to debug and they (hopefully) win a patch for the bug :-)

Lawrence Oluyede
A: 

Debugging is 70% about finding and figuring out the bug from reports of anomalies before you can do anything about it, and 30% about figuring out how not to take the castle of cards down when fixing it. If you have it pointed out in the code for you, or are just given code and you're told it's buggy, you're in a worse place than where you started.

Lawrence's comment is spot on IMO, go hunt down something that's been tracked and logged and you have rero steps for in a project where you have realistic constraints and enough codebase to work with if you want this exercise to have any meaning. Will double as good exercise in learning to read too when you have to puzzle out an alien code base too.

ThE_JacO
A: 

Here's a nice example, spot the bug ;)

Just a bit of code that bit me a couple of years ago.

methods = []                       
for i in range(10):                        
    methods.append(lambda x: x + i)        

print methods[0](10)                       
WoLpH
Ah, that's a little bit tricky. I've run into way too many problems like that myself, and I never seem to learn from this particular mistake. :) Hint since the OP is new to programming: read about closures/scoping.
bcat
That's gone a bit over my head :P. But I never knew you could use `lambda` like that. I've only made very limited use of it. But I see that every number for every `method[n](10)` it gives a 19... No idea.
vlad003
The problem is that `i` is not a local variable to the `lambda`. So it's getting `i` from the parent scope, which will be the last number the `range(10)` will return. It is an important lesson to learn, how scoping works. One way around it would be to do this instead: `methods.append((lambda y: (lambda x: x + y))(i))`. That way you add the value to a local scope of a wrapping lambda function.
WoLpH
A simpler way round the problem is `lambda x, i=i: x+i`. The parameter i will be initialised to the current value of i in the loop.
Dave Kirby
@Dave Kirby. That's also a nice solution. The only problem with that is that you change the signature of the function, that may or may not be a problem depending on the case though.
WoLpH