Thursday, June 11, 2009

Why Choose IronPython over C#?

Matthew Harelick just asked (on the Python.NET mailing list of all places) why he should consider using IronPython:
My background is primarily UNIX. I have been programming in C, TCL, Perl, and Python in UNIX and Linux for a long time.

I recently started working in Visual Studio and C#.

Visual Studio and C# make everything very easy. Python was nice when you didn’t have the convenience of Visual Studio and intellisense with a nice debugger.

In the windows world I see no major reason not to use C#.

Why would I want to use IronPython?
A lot of C# developers feel the same way, why should they consider another language? Here is my answer:
Both Python and C# are 'general purpose' programming langugages suited to a wide range of problem domains. Choice of programming language is therefore usually a matter of aesthetics, what you are used to, personal workflow, issues within a team and so on rather than a matter of which is more appropriate to the job. Obviously there are exceptions where one language is obviously more appropriate or obviously inappropriate but these are the exception rather than the rule.

Place where you should obviously use IronPython:
  • For embedding as a user scripting language / plugin mechanism or are doing runtime code generation
Place where you should obviously use C#:
  • You have already implemented in a dynamic language, profiled and optimised, and need faster performance than the dynamic language is capable of
Other than that the choice is yours!

Here are a few reasons why I / some people prefer IronPython:
  • The interactive environment is an invaluable tool for exploration
  • No need to create a whole 'object-oriented-application' for simple tasks where it is not needed
  • Without a compile phase and with duck typing / dynamic typing IronPython makes it dramatically easier to do test driven development or even normal unit testing
  • Python has many language capabilities missing from C# which make it a flexible and concise language. These include:
    • Duck typing and protocols (no need for explicit interfaces)
    • Heterogeneous containers (no need for generics)
    • Pain free introspection (no need for the complexity inherent in System.Reflection)
    • Metaprogramming including good old eval, type creation at runtime, decorators and descriptors
    • Late binding and runtime type dispatch
    • 'Little features' like tuple unpacking, returning multiple values, everything is an object (including primitives, functions, classes and modules)
There are things that C# has that Python doesn't (compile time checking, method overloading, access to .NET attributes), but choose whichever you prefer.
I've written about this subject before (and at a bit more length) in Dynamic Languages and Architecture.

Other reasons I didn't think about the time:
  • If you are starting out Python is much easier to learn than C#.
  • As a concise and expressive language it is faster to prototype solutions in Python and faster to turn those prototypes into full applications!
  • Writing portable, cross-platform code. Compatibility with CPython is a great thing.
  • Availability of open-source libraries. There are orders of magnitude more open source libraries for Python than there are for C# despite C# being used more than Python in commercial settings.
  • Python is the 'standard' language in various problem domains (GIS and animation scripting, Linux system administration and certain types of scientific programming for example).

1 comment:

  1. Nice summary of the pros and cons, Michael.


Note: only a member of this blog may post a comment.