Copyright © 2012 Tom Coote. All Rights Reserved. Powered by WordPress themed by bavotasan.com.
I’ve been programming in C# on a daily bases for 5′ish years. Over the past 2 years I’ve also been programming heavily in Python. I use both languages for the same thing which is to build web applications and web sites. The only thing that made me learn Python was Django because I wanted a framework to help build web apps and there is no framework in C# that comes close to the usefulness and flexibility of Django.
I don’t dislike C#, I think it’s a very powerful language but having now worked extensively in Python I can safely say I prefer Python for the purpose of building web applications. Here is why.
Ways to do things
When you want to get a job done in Python there always seems to be just one obvious way of doing it. In contrast, if you use C# then you’re faced with a varying amount of choices to achieve the same goal. Choice is good right? No! It’s difficult to read code, especially someone else’s. It’s made even more difficult in C# when you’re faced with the variety of data types and syntax that could have been used to achieve the same job. It’s so much easier when your preconception as to roughly how something would have been achieved is more often than not an accurate one. I’m not saying that there isn’t variety in Python, I’m saying that the whole language seems to be designed, presented and documented in such a way that more often than not results in the same choices being made when producing a solution to a problem.
Syntax
Syntax is the next logical thing to talk about. If you’re learning Python and doing a lot of reading around the subject you may well get fed up of reading about how Python syntax is designed to be readable, as if English phrases have been used to describe code. Well it’s true! As well as this, I have been astonished at times at how little syntax is needed to achieve something in Python than it would have in C#. It’s also about elegance, readability and making the job easier to achieve. For example:
Python:
if 'e' in 'hello':
return True
C#:
if ("hello".indexOf("e") >= 0)
{
return true;
}
Referencing
I’ve never enjoyed how C# handles it’s references to external libraries of code. I don’t think there is a C# programmer out there who can’t recall a time when they have wasted a whole day just figuring out referencing problems in a C# solution. Again, I think the problem is the variety of options. You can make references inside the applications config file or at a project level using visual studio. Each reference can be to a DLL on the file system, another project in the solution, a library that is located in the global assembly cache or a COM object (I don’t know where COM objects exist?). It doesn’t end there, once a reference has been established, your code file needs a using statement to say what namespace from your reference is being used in the code. There is so much opportunity for things to go wrong here that there is no wonder it can get very messy, very easily. So what’s so great about Python referencing? It’s the fact that there is one way to do it and mistakes are obvious. You say what external code you wish to use in your own code file. That code is then found from the file system, either checking in locations relative to your code or checking in locations on your Python path.
Functions
I enjoy functional programming, it’s why I like JavaScript so much. Pythons use of functions is the best I’ve ever seen. If you’re into the DRY principle, and you should be, then you will know that it is often the case that one piece of code can be used again and again by either changing the input or the output to the same code. So to highlight one way of achieving this with C# you have function overloading which works well and looks like this:
public int MyFunction(int x, int y)
{
return x + y;
}
public int MyFunction(int x)
{
return MyFunction(x, 5);
}
So in this example you have a function that can accept two different inputs but the same bit of code is being used to do the actual work. Here is the Python version:
def MyFunction(x, y=5)
return x + y
I’m not going to get into explaining syntax but I can safely say that the Python solution is more elegant, more easy to read and less typing. It’s examples like this throughout Python that continually reinforce my preference towards the language.
Python dynamic types vs C# static types
Okay, so this is an old argument and I don’t subscribe to the thinking that one is better than the other. However, I do have the thinking that one type system can better suite a programming need than the other. When it comes to a web based environment I strongly believe that dynamic types are the best choice. For those of you that are using the most recent release of C# you will know they have started to bring in dynamic types. However, this is yet again adding to the set of features that make the language so difficult to use in the first place, more choices more confusion. The web is dynamic and data that is passed between the layers that make up a web application is handled differently at each layer. From the DOM to JavaScript, then from JavaScript to the HTTP and from there to your programming language of choice. That data has been traversed through all those layers but if C# is your programming language then you’re in for a lot more work to accept that data because it needs to fit into predefined data types. So pick the right language for the job.
Introspection & Reflection
I’ve used these concepts a lot in C# and it’s just difficult. I think it’s inherently a bit more difficult given the extra need to maintain the static types in C#. Once you get into introspection of Python code you soon realise how easy it is in comparison. The ability to manipulate code at runtime in Python is built into the language where as in C# it feels very separated. Like here is C# and over here are all the things you’ll need for introspection, reflection is a bit easier. Added to this, I think all the things that I have mentioned so far all come together here and make introspection & reflection in C# mega difficult. I just don’t want to have to spend the time wrestling with it when I can choose Python.
Conclusion
The list of comparisons between C# and Python is vast and those points I’ve discussed above have only been briefly described. But the reason I choose Python is because I think it’s about choosing the right tools for the job. I do the things I’ve mentioned in this post on a daily basis and Python is the language that makes it easy. I’m not involved in desktop applications, operating systems, game development… etc etc. I build web apps and my language of choice for doing that is Python. C# is more than capable of building extremely good web apps but I can assure you the developers have had a harder time achieving that than if they were to have used Python. I know that because I’ve spent years doing both.


January 24, 2011 at 10:57 am
‘Python’ is also the much cooler sounding name.
February 25, 2011 at 4:45 am
Oh, god…
if (“hello”.Contains(“e”))
return true;
public int MyFunction(int x, int y=5)
{
return x + y;
}
dynamic foo = …;
foo.AnyName();
February 25, 2011 at 9:16 am
What is going on here? I take it the … is the rest of the code that has to exist?
February 25, 2011 at 9:52 am
Oh, Sorry. ‘…’ means it can be any class.
I’ll rewrite that.
class Foo { public void f(){} }
class Bar { public void f(){} }
dynamic a = new Foo();
a.f(); //OK
a = new Bar(); //OK
a.f(); //OK
a.HelloWorld(); //Runtime Error. Not Compile Time
Your code is C# 2.0 and now we have 4.0.
(I know python is still easer)
February 25, 2011 at 10:10 am
I’ve not delved to much into C# 4.0 only via reading mostly. It has some nice features…. more nice features.
March 17, 2012 at 10:35 am
You geeks think too much about programming …
Lee Chun, write a book ..
i think Tom the issue is the .NET framework and not C#, Good thing about C# is that every year it (Mikrosoft) copies bits from everyone else. Funktional prog, Lambdas, Generics etc… but sometimes “They” (Mikrosoft) “Plagarise” too much and copy stuff and change it again, XML DataSets vs Entity Framework …. Linq2Sql vs LinqToEntities.. and stuff dosen’t work initally and then version 4 comes out and it does ….
Mikrosoft Sucks ….
July 17, 2011 at 8:33 pm
Much of the negative examples here of C# are wiped out in C# 2.0 and 4.0 and I see little gain in Python there. Personally I find C# as readable, if not more so. I find Python a little “geek hacker” in style.
I’m no fan at all of dynamic typing either. Yes it may make your life seemingly easy, but also lazy and allows for errors to sneak in. Worse at run time. A web app may seem like more of an ideal application for a dynamic interpreted language, but then that also makes it a good target for hacking if the developer doesn’t actually understand what’s going on under the hood or the consequences of actions they didn’t expect. The problem with many high level “easy to use” languages is they are attractive to managers who can hire cheap developers that can cope with an easy to use language without much experience or knowledge.
Strongly typed languages can be a pain for sure, but they really make sure you are paying attention to your data and reject anything at build time that isn’t correct.
July 17, 2011 at 9:36 pm
I do agree with your point that dynamically typed languages allow for lazy programming which results in errors creeping in. JavaScript has some of the biggest examples of this… especially in the early days of JavaScript. That’s a bit of a people problem though, if someone is not competent and/or a lazy programmer then code will always be bad. Equally, an incompetent programmer can use the compiler for a strongly typed language to get their code working without actually fully understanding what’s going on. You end with different problems but problems none the less.
If an employer is willing to hire cheap skills then they have a bigger problem than anything I’ve discussed in this blog post! I’d say hire people who really make sure they’re paying attention to programming concepts and you’ll have fewer programming problems no matter which language is used. I’ve seen beautiful and ugly coding in both C# and Python, some of both sorts written by myself.
August 25, 2011 at 1:16 am
I just have to say, you forgot the old maxim “perfection is reached not when there is nothing left to add, but when there is nothing left to take away”.
When this is applied judiciously, your note about C# being so non-uniform actually falls on it’s face as people using good design principles tend to right surprisingly similar code, for example:
return “hello”.Contains(“e”);
functionally identical to your code, I just remembered to continue taking things away until there were none left that could be removed.
December 13, 2011 at 8:20 am
They’re not functionally identical. What you probably meant is that “hello”.Contains(“e”) is functionally identical to “hello”.IndexOf(“e”) >= 0.
There’s certainly less cognitive overhead required to understand “Contains” compared to the cryptic “IndexOf >= 0″. If one adheres to Python’s philosophy of “There should be one– and preferably only one –obvious way to do it.”, and knows (at a conscious level) about the Contains method, then there’s one obvious way to do it in C# too, for this specific instance.
I suppose one can potentially learn about using the former over the latter by reading the API (who has the time anyway?), using Visual Studio’s IntelliSense (‘C’ is lexicographically ordered before ‘I’), reading other people’s code, through pair programming or code review, using refactoring or static analysis tools (which can probably fix it, but one should manually understand/review the changes anyway), or reading blog posts (like this). Perhaps it’s just due to Coote’s experiences with other languages that only have the IndexOf method for strings, and that he haven’t had the aforementioned opportunities.
This example aside, I’m very much tempted to try Python for the dynamic typing, cleaner syntax and most importantly to experience first hand the “one obvious way” philosophy.
January 20, 2012 at 6:21 pm
…not functionally identical if “e” is not found.
December 15, 2011 at 5:00 pm
you can just write which will return true:
‘e’ in ‘hello’
Also you could do
def my_func(str_1, str_2):
return (str_1 in str_2 or str_2 in str_1)
will return true if str_1 is in str_2 or vice versa. and will return false in all other cases
March 14, 2012 at 11:50 am
Both C# and Python are powerful languages… but i must say when i was noob at programming i used Python for GUI parallel port interfacing and its far much easier then in C#…….
May 1, 2012 at 5:04 am
The other thing about ‘e’ in ‘Hello’ is that this functionality is slightly different to say for i in range(10): . Here i is not defined yet but gets assigned iteratively each individual element thats in the range.
Having these two different conditions imo can lead to and cause confusion as well as reduce readability to a certain extent.