Sunday, August 23, 2009

Multiple Inheritance in IronPython

Python allows for multiple inheritance, and therefore so does IronPython. With multiple inheritance you provide multiple base classes. As member resolution is done dynamically in Python, when you call a method the class hierarchy is searched to find the correct method. With single inheritance this is straightforward (the inheritance hierarchy is a straight line), with multiple inheritance the method resolution order is used to find methods.

Multiple inheritance can be very useful where you really need it, but it also complicates your code. I believe it should be used rarely, but there are plenty of Python programmers who believe it should never be used. The most common use of multiple inheritance is to provide mixin functionality. It is often used in a similar way to interfaces in languages like C#.

Simon Segal has written a blog entry describing multiple inheritance in IronPython, and how it interacts with inheriting from .NET types:
Question: Does IronPython support Multiple Inheritance / Mixins?

Ans: Yes!

Question: Can I use my C# classes from IronPython (in the IP runtime)?

Ans: Yes!

Question: So, I should be able to use my C# classes in a multiple inheritance scenario from IronPython?

Ans: Yes!
In IronPython you can create Python classes that inherit from .NET types and other Python classes, but you can't simultaneously inherit from multiple .NET types. This allows you to create C# classes that you use as mixins from your IronPython code.

No comments:

Post a Comment

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