Archive

Archive for August, 2009

C# classes for Mixins and Multiple Inheritance with IronPython

August 10th, 2009 Simon Segal 2 comments

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:

multi_inheritance_using_csh

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

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.

mutliple_inheritance_tipas

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:

widgetA-B-C_ironpython

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)!

Share/Save/Bookmark

Categories: C#, IronPython Tags: ,

Doing .Net Days has a logo.

August 9th, 2009 Simon Segal No comments

Thanks to the kindly offered help of BOUNCE (check out his work), “doing .Net LOGOS_FOURdays” has a logo! If you didn’t know, “doing .Net days” is a monthly community training event run by TechAvalanche once a month.

Doing .Net Days will focus on a variety of programming topics, mostly as they relate to programming on the .NET platform and covering technologies that touch both the CLR and DLR.

Doing .NET Days will also give a great deal of it’s ‘Air Time’ to topics that feature heavily in the Alt.Net space. Open Source frameworks and tools will also be covered extensively and if you have stopped by here before you would know that our very first event will include a discussion and demonstration of nServiceBus, a framework (fast growing in popularity) designed to make building distributed systems easier.

Share/Save/Bookmark

Categories: doing .Net Days Tags:

The IronPython standard libraries dilemma – [a learning strategy]

August 5th, 2009 Simon Segal 4 comments

The choices facing any programmer wanting to start working with IronPython or IronRuby for that matter, contain some interesting questions. Working with Dynamic languages in .NET prompts us to consider whether to use Python / Ruby native standard library method and functions or directly work against the BCL? If your programming in a statically typed language on top of the CLR, then it’s just different syntax but the same platform libraries. If you want to try your hand with IronPython or IronRuby you need to consider that both these languages have evolved on different platforms. For example, should I consider using Python standard library functions to interpolate some strings or should I use the BCL? Consider these two approaches:

Classic Python Version

print should I use python functions or .Net? +
                    Ans = %(yes)s % {yes: yes}

BCL Centric Version

print String.Format(should I use python functions +
    or .NET? Ans = {0}, BCL Functions)

So that was pretty trivial. What about File access?

Classic Python Version

 rdrp = open(r”c:\Document1.txt)
 rdrp.read()

BCL Centric Version

 rdr = StreamReader(r”c:\Document1.txt)
 print rdr.ReadToEnd()

Any clearer on a choice? The pragmatist in me says use the BCL and the purist says use the python standard libraries. So how heavy should our BCL wielding hand be? This leads me nicely to the next question, how exactly should we go about learning a Dynamic .NET Language? Surely this process will help us make up our minds about when to use the BCL and when not to? Something tells me that the answer lies pragmatically somewhere in the middle ground? The kind of meat and potato stuff like that demonstrated above is likely going to have me opt for the classic Python approach as my default position, however I am not going to put myself to the trouble of working with databases for example without the help of ADO.Net. To help myself form my own opinions I decided that the best learning path for me was as follows:

  1. Read the Python 2.5 (CPython) documentations PDF Tutorial and re-type it’s code examples and follow with my own experimenting.
    • This allowed me to get a fairly good overall feeling for the language.
  2. Read IronPython in Action to get a good overall point of relevance to .NET and see how the power of .NET as a platform would impact the Python experience and visa versa.
  3. Read Python is a Nutshell and get a grasp on Python’s roots and how the language is best put to use on it’s original platform (the C implementation).

Some people will surely think that points 2 and 3 should be reversed? Perhaps, but my rationale for taking this approach is that Python is not likely to become a daily instrument in my work and I want to understand as much as possible about it in the .NET world first. I do believe however that to truly understand the power of Python I will need to learn deeply about its roots and oddly I suppose that step in the process will come last.

Share/Save/Bookmark

Categories: DLR, IronPython, IronRuby, Languages Tags:
Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia