see whatever…

jump to menu

September 27, 2007

closures in Python

Filed under: Javascript,Python — see @ 9:58 pm

Written quite a while ago but but still a very good post about how closures in Python work. Basically they are read-only so if you need to mutate a value you need to use a mutable variable like a list or object.

Closures in e.g. Javascript are easier – ah wait, actually I am not quite sure if I actually did change a value the last time I just a closure…

- just checked, it does work as expected (thanks to Firebug, I guess that’s why I like both Javascript and Python, both with a great console ;) ):

>>> a = 1
>>> function b() { alert(a); a = 2}
>>> b() // alerts 1
>>> b() // alerts 2
>>> a
>>> 2

I do quite like to work with closures, working with Ajax callbacks is almost only possible by using them. I did try to use the same technique in cssutils and of course failed miserably until I found the above post and workaround. So all in all better than nothing. I wonder though if it would be very difficult to add “proper” closure support to Python, guess a simple syntax is the most difficult…

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress