Possibility and Probability

Python, AI, and other fun stuff

5/27/2004

50 Foot Wave

I'm a big fan of a lot of Boston area bands like Throwing Muses, Belly, Juliana Hatfield, etc. I've been following Kristin Hersh for a while now, and she has a new project called 50 Foot Wave. They are incredible. Seriously.

50 Foot Wave is planning to release several small albums a year and tour a lot (50 to 100 dates a year). The name pretty much describes the sound. Its a three piece band rocking out at full throttle. You can download (MP3) their 6 song EP/album for only $6 from their website.

Check them out, they are sooooo cool. I'm a big fan of the idea of independent music, and Kristin really represents that idea well. My favorite tune at the moment is Clara Bow. For me right now, this is a serious contender for the best album of the year. (My other choice would be Incubus's "A crow left of the murder" which is just rocks to no end.)

Comment on this post [ so far] ... more like this: [music]

Global variables and my confusion

I have only been programming in Python for about 5 months, so I am still fumbling my way through things. Last night I discovered that my understanding of global variable scope was way off. This code snippet illustrates my problem:

amount = 12
price = 23

def function1():
        amount += 1

def function2():
        amount = price
        amount += 1
        print "Amount is: ", amount

def function3():
        global amount
        amount += 1
        print "Amount is: ", amount

if __name__ == "__main__":
        function1()
        function2()
        function3()

When I would try to run code like function1, I would get an error about "local variable 'amount' referenced before assignment". And this confused me to no end. From my C/C++ and Java background, it certainly looked like amount was in global scope, yet this error clearly stated that it wasn't. Instead of looking it up in my handy python book, I implemented something like function2.

In function2 I would basically assign amount to be some other global value and then go and do my operations on amount. Yet when I left function2 and tried to use amount somewhere else I would not get the value I was expecting. Amount was not being used in a really critical section of code and there were other issues to deal with so I commented it out and decided to come back to it later.

Well, last night was later. After about 5 minutes of looking at it (catching up on what the issue was) I pulled out the book and discovered to my surprise that you have to declare that you are going to use a global variable. function3 is an example of the proper usage of the amount global variable.

At first this really irritated me, I thought that by virtue of the variable being suddenly assigned a value would cause Python to look into other scopes to see if the variable was defined in other places. That's what Java and C/C++ seem to do.

But I can't decide which way I think is better. In layman's terms, Java is assuming the variable is global and Python is assuming it's local. Ok, that is an big oversimplification, but that was how it seemed to me at first. Strong typing vs weak typing. Static vs Dynamic. Compile time vs Runtime. I've never really thought about these issues in this way before. There are arguments on both sides.

At any rate, I've got much thinking to do about this topic. That and I need to go look through my code to make sure I'm not "getting away with murder at compile time" as Josh Bloch put it.

Comment on this post [ so far] ... more like this: [python]