Tonight, I installed Visual Studio 2010 beta 1 for the first time. I know I am late to the game, but I had to give it a whirl since I found some free time this afternoon. One feature of .NET 4.0 I wanted to try out was the dynamic keyword with Python scripting. So, here is my first hello world program using dynamic, with the help of this walk-through.
class HelloWorldDemo: def run(self): return "hello world"
Here is the C# code needed to run it. Yes, I used WPF as my driver, but don’t worry about that.
... using IronPython.Hosting; using Microsoft.Scripting.Hosting; namespace WpfWithPython { public partial class Window1 : Window { ScriptRuntime py; public Window1() { InitializeComponent(); py = Python.CreateRuntime(); } private void MyBtn_Click(object sender, RoutedEventArgs e) { //Instantiate an instance of this file?! Yes, it is weird at first. // All the methods and classes in the file will be members. dynamic demo = py.UseFile("HelloWorldDemo.py"); //This is the tricky step. To create a new object, call it like a member... // I think dynamic instance = demo.HelloWorldDemo(); MyText.Text = instance.run(); } } }
It took me a while to get this little bit of code working unfortunately. If you read the comments, you can see where some of my confusion was found. Overall, it is pretty awesome. I loved LINQ and I am already loving dynamic. There is something new to learn everyday. Maybe F# next…
Unfortunately, I have to say that VS2010, while pretty, is slower than 2008. And, IronPython took nearly 2 or 3 minutes to run some sample code it provided, which is crazy. If the performance problems can be overcome, then I see myself learning more Python and Ruby.


Sunday, July 19th, 2009, 10:33 pm | 



August 7, 2009 at 6:41 am
Great article … I’ll go ahead and installe the betaversion also, have to check performance, if it really is that slow …
March 6, 2010 at 6:22 pm
Cool Thanks for this post. I am just starting django and this is a big help.
March 6, 2010 at 6:29 pm
This is great! Thanks for your article. I am a newbie at python and this got me straight.