Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Sunday, March 11, 2012

Using decorators and closures in python

Decorators

A decorator wraps a class or method to extend it. In python the syntax used to wrap the original function is @decoratorname. For example let's say the original function A needs to be decorated with B:
def B(a):
  def C(*args):
    return 2 * a(*args);
  return C

@B
def A(x, y):
  return x + y;

print A(2, 3)

Wednesday, February 8, 2012

Fixing Sublime Text 2 for a Japanese Windows system

Sublime Text 2 is a programmer’s text editor with build-in Python scripting. Usually I use Programmer’s Notepad as a lightweight text editor, but I decided to give Sublime a try.

Wednesday, February 1, 2012

Modifying python classes at run-time

Adding methods to python objects and classes at run-time can be very handy when API specifications are fluid or when loading custom file formats. Think for example of an object implementing a protocol defined as XML or another descriptive language. Or an object giving access to stored database methods. Instead of only having a general execute method or having to recreate a static API file, all the described methods can be added to a class at runtime.