Python like .NET

17
Apr/09
0

Python allows you to assign almost everything.  There doesn’t seem to be a difference between variables and functions.  You can have a class Foo with attributes bar and baz. These can be methods or plain varaibles, and you can re assign them at run time.   So Foo.bar = 10  and Foo.bar = { return a+1 }  are both valid (though not valid python syntax).   This allows you to reassign methods/functions at run time.

.NET has a simular feature called Delegates.  These are basicly pointers to function.  Most people will use these for callbacks and events.  Something interesting you can do is use them in properties.

class Foo
{
  delegate void Fn(string s);
  Fn bar;
  Fn baz
  public Fn Bar{
    get{ return bar; }
    set{ bar = value; }
  }
  public Fn Baz{
    get{ return baz; }
    set{ baz = value; }
  }
}

Using this class you can now assign the class methods at runtime.

Calling the method:

f.Baz("something")

and reassign the function to print out a line

f.Baz = f => Console.WriteLine(f);

The class looks just like it has normal methods, but they are not assignable.

Filed under: Uncategorized

Fun with timezones

9
Apr/09
0
Filed under: Tech

Swap with out the tmp

27
Jan/09
0
Filed under: Code

Launch Wrapper

2
Sep/07
0
Filed under: Tech

White Screen of Death

18
Jul/07
4
Filed under: Uncategorized