Hidden similarity between instance level methods in Python and C#

By | May 31, 2015

So I am brushing up on my Python skills because I want to improve upon my knowledge of scripting languages. I code in C# more often than not which is a statically typed language and I wish to expand my horizons by getting more serious about dynamically typed languages.

One thing that I noticed while going through some sample tutorials is that we define instance level methods using a reference to the instance object as the first argument using the conventional variable name of self.

To some programmers (especially those from a .NET background) this may seem a bit cumbersome. However, if we define the equivalent class in C#:

The C# compiler will turn this C# code into CIL (Common Intermediate Language) code before it is eventually generated into native code (a couple steps later) for execution by the CPU.

For the Greet method, the following CIL code will be generated:

For those who are unfamiliar will CIL code, I have commented the non-‘no op’ lines for you. The particular point of interest is that the “name” argument actually exists at index 1 of the method’s arguments – So the question is what exists at index 0? Well index 0 contains a reference to the current object (think C# keyword this). In fact, every non-static method receives a reference to the current object as an implicit additional parameter at index 0 of the incoming arguments.

So why is this significant? Well it means that the .NET Framework in a sense does require that for instance level methods that a reference to current object is passed into the method. This is much like the requirement in Python, however, Python just happens to be much more up front with us about it. Instead in the .NET Framework, it is done behind the scenes and is visible to us in the CIL code (although somewhat indirectly yet). Python is designed with the idea that explicit is better than implicit.

Note that for static methods in C# there is no implied first parameter as a reference to the current object in the CIL code. Likewise, in Python you can make a method implicitly a class level method by omitting the reference to the object instance as the first argument to the method, however, the practice is somewhat not recommended.

It is interesting to see a commonality between these vastly different languages where it is not immediately obvious that there is one.

Leave a Reply

Your email address will not be published. Required fields are marked *