Python like .NET
Apr/090
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.
No comments yet.
Leave a comment
No trackbacks yet.