Chapter 14. Extending IronPython with C#/VB.NET

 

This chapter covers

  • Creating class libraries for IronPython
  • .NET attributes and IronPython
  • Calling unmanaged code with P/Invoke
  • Creating dynamic objects with C#/VB.NET
  • Compiling assemblies at runtime

There is a standard mantra about performance when programming with Python (CPython). The mantra goes something like this: “Code in Python first; then profile and optimize your bottlenecks. Finally, if you still need your code to run faster, rewrite the performance-sensitive parts of your code in C.” Okay, as mantras go there are probably snappier ones, but it still contains a lot of wisdom. Of course, with IronPython we wouldn’t drop down to C[1] but into C# or VB.NET instead.

1 Well, not usually. There are tools like SWIG that will allow you to generate .NET wrappers for C/C++ libraries.

This is an area where IronPython has a great advantage over CPython. Both C# and VB.NET are modern object-oriented languages with garbage collection and all the features of the common language runtime, and both are substantially easier to work with than C. In order to write a Python extension in C, you have to manage your own memory and use the Python C API to create and interact with Python objects. By way of contrast, we have shown how simple it is to use .NET classes from IronPython, and this includes classes you write yourself.

14.1. Writing a class library for IronPython

14.2. Creating dynamic (and Pythonic) objects from C#/VB.NET

14.3. Compiling and using assemblies at runtime

14.4. Summary

sitemap