C# classes for Mixins and Multiple Inheritance with IronPython
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!
Making it all work
If you create a class in managed C# you are free to pass it along for use to the IronPython runtime and thus access it using IronPython. Let’s take the following class created in C#:
public class Person { public string Name; }
Using my IronPython WPF interactive console control, I can interactively create new classes with IronPython and use my C# Person class as the base class in an inheritance object graph. Here’s the code as typed at the interpreter:
As you can see, first up I imported the relevant namespace (module) where the C# Person class is defined. The first class derived from Person is Employee and then I have derived WarehouseManager from Employee which leaves our Inheritance tree like this:
- Person
- Employee
- WarehouseManager
- Employee
You can also see that both the derived classes have access to the Name property of the C# Person class which proves that the Person class has been included in the inheritance of the Employee and subsequently WarehouseManager classes.
So that’s some plain old inheritance using C# classes. Now lets take a quick look at multiple inheritance using the same C# class.
When should I use this feature?
Probably never according to some people, yet for every objector there seems to be fan. Languages that do support multiple inheritance, often come with a disclaimer for programmers to avoid their usage and Python would appear to be no exception in this case. The above is a good example of why multiple inheritance isn’t necessarily a good thing, given that a manager is presumably an employee and at the very least a person. Nonetheless it demonstrates the intent and this post isn’t so much about the ‘why’ but mostly for now it’s about the how. I will follow up soon with more about the ‘why’ and ‘when’ in a subsequent post.
Carefully managed, I can see it’s potential uses could be enticing. Yes, yes I know “composition over inheritance” etc…There is never one shoe fits all!
Just in case you where wondering
If you were thinking of doing something like this in C#:
public class WidgetA { //….etc } public class WidgetB { //….etc }
Followed up by:
You will see that we cant in fact use two classes created in a CLI compliant language. This will effectively stop crazy stuff like this:
class MutantUi(Form, Button): def SetTitles(self): self.Text = “What happens here“
But I am completely free to do this:
from Org.TechA.Wpf.ControlsHarness import * class PythonMixin(object): def method(self): print ‘method‘ class Mutant(PythonMixin, Person): def MutantMethod(self): print ‘mutant method‘ m = Mutant() m.method() m.Name = “Simon“ print m.Name m.MutantMethod()
So there you have it, Multiple inheritance / mixins with C# (well kinda)!
2 Comments so far
Leave a reply









Your first example doesn’t use multiple inheritance (just before you say “When should I use this feature?”).
Is this intentional? Is this section just intended to demonstrate inheriting from C# classes in IronPython?
[Reply]
Yes it was intentional, I was building up to it. Perhaps I should have included the multiple inheritance example first up and made it obvious from the outset. Or perhaps the heading should read “When and how should I use this feature”…
[Reply]