Categories
python

Python scoping

Last friday, Lau discovered an interesting edge case of python. Something that at first appeared to be a bug, but later revealed a deeper truth about the scoping of Python. I guess coming from a C++ background spoils one in some regards. Scoping in C++ is simple and uniform, I have never in many years of programming C++ got bitten by the scope of the language, something that I also can’t say about C#.

The problem boils down to how Python treats local variable and global variables. The following example, stolen and modified from here, shows the edge case in its most simple form:

a, b = (1, 2)

print(a, b)

def test():
    print(a)
    print(b)    # (A)
    #b=1       # (B)

test()

(A) works as long as (B) is commented out. The strange thing is that changing (B) into += produces the same result. Actually it is the case that += a global variable will never work, unless you have declared it in the local scope first. This comes back to the way Python threats variables in the local scope. The following link has more examples to illustrate how scoping works and also how this is different in Python 3k. As explained on Stackoverflow, using the dis functionality sheds some light on strange cases like these. All this said, the scoping has one good thing going for it. The following is quite useful and perfectly legal in Python:

i = 0

def ex1():
if i < 0:
b = 10
else:
b = 20
print b

ex1()

Categories
funny My Media System On the web python

Lolcats in your media center

I wrote that in about 3 hours thanks to the wonderful support in MMS for writing scripts in python.

Categories
My Media System nokia python tablet

My Media System 1.1.0 rc1 released

It’s been a long time coming but finally 1.1.0 is almost here. The new stuff is: plugins, improved search, opengl output with fancy transition animations, python support, movie collection. ofdb.de support, last.fm support, improved lcd support and new theme. More here (pictures :-)).

I did a small video to show how one can use a nokia 770 (the n800 works even better) together with MMS that you can see here.

Acmelabs did a small video of the opengl plugin in action:

Categories
boo c# lisp Programming python

The power of lisp part 2

As mentioned earlier, lisp is incredible at abstracting away small everyday stuff you write over and over. A good example is this way one has to define a local variable in order to make variables sucked into a delegates from the surroundings static in C#. As mentioned briefly in this blog post, one needs to change the loop to read like the following code for it to print 0 to 9 instead of 10 times 9:

foreach (int i in numbers) {

        int j = i;

        l.Add(delegate() { System.Console.WriteLine(j); };
}

In lisp one would see that this is a pattern and simply abstract it away like this. As one can see, the macro static-loop is more or less exactly the same as the pattern which luckily made it quite easy to write.

(defmacro static-loop ((i container) &body body)
 (with-gensyms (j)
  `(loop for ,j in ,container do
    (let ((,i ,j))
     ,@body))))

The only small trick is the with-gensyms macro which prevents leaking the temporary variable j into the scope of where it will be substituted into. So in that regard the macro is even nicer than rolling your own temp variable hack 🙂

For reference the python example works the same way, but allows one to use the lambda function to redefine variables, although one must note that the static-loop solution is more general in that it will make the loop variable local for the whole scope and not just for one lambda function. Funny thing is that I had originally written it like this, which actually worked, although that means that the closure binding is not only by reference but by name.

I tried writing the macro in boo but I failed. Apparently their macro support is still quite new and there’s close to no examples to draw inspiration from. If anyone could help me out with the solution please mail it to me or even better add it as a comment.

Categories
Free Software python

Python for Series 60

Just found out today that its possible to run Python on Series 60 phones. How insanely great! I really need to start experimenting with coding something for my phone now. Already a great free software game for them 🙂