Worst python code I've ever written
I was doing my homework for the bioinformatics class when I started experimenting with scope in Python. The first thing I noticed was that Python list comprehensions don’t create a closure. This means that variables defined in the list comprehension bleed out into the current scope:
I’ve combined this with the fact that functions have access to variables from the same scope, and come up with this example:
At first glance it doesn’t seem like there is something horrbly wrong. But when you try to figure out what the
function actually does, you immediatly find something peculiar. There is function call in the list comprehension that
takes no arguments. So why am I defining all those variables (
tj) which aren’t being used?
I don’t even need the enumerate calls, right? Wrong! All those variables are being put to good use inside the
function! Yes, the cost function has access to all those variables and hence doesn’t need any paramters at all.
When I showed this peace of code to my friends they all thought I was insane. We ended up agreing that code should not be written like this as it makes your program very difficult to understand.
Just for kicks I included these two lines in the homework report as well:
I am a horrible person.
By the way, the homework assignment was to compare COX3 mitochondrial gene between multiple species and build an evolutionary tree. Here is the resulting dendrogram with avrage linkage:
I’ll probably post the full source code once all the assignments get graded.