Python vs. C#

Hannu Kankaanpää hanzspam at
Sun Aug 10 11:43:09 CEST 2003

"Brandon J. Van Every" <vanevery at> wrote in message news:<3f357a9b at shknews01>...
> So again my question is, language-wise, what can I get done with Python that
> I can't get done with C#?  What is easy to express in Python, that is
> tedious, difficult, or impossible to express in C#?

There aren't many single cases where Python can express things 
considerably better, but in general Python leads to about 50% of the code
that you'd end up with in C# (actually, I've only had this experience
when comparing to Java or C++, but C# is quite similar to Java).

First, I'll give some of the real examples that can't be expressed
in C#. I can make a memoize()-function in Python, that remembers
what arguments were passed to a function and what the return value
was, so calling again with the same arguments is but a simple
hash table look-up.

def fib(n):
    return n < 2 and 1 or fib(n - 1) + fib(n - 2)

Everyone knows what kind of recursion nightmare that is. But
let me add this line:

fib = memoize(fib)

Now fib() calculates fibonacchi numbers in linear time and
for numbers that were already calculated, it's O(1) (average case,
mind you. hashes can be slow when unlucky). I'm not an expert
in C# but I still dare to say you can't make such a generic
function as "memoize".

Python has some other features not found in C# (AFAIK, once again)
that can lead to considerably cleaner and shorter code. At least 
metaclasses and generators. With metaclasses you could e.g. make
fully automatic class registration, so that each subclass won't
need a piece of code to register itself. With generators you
can make itearion over some complex system very simple, such
as iterating through a directory and all it's subdirectories. In many
other languages, this has to be done with cumbersome callbacks.

Then there are the typical things that make Python code smaller,
like dynamic types (no need for interface classes at all, or explicit
declaration of types), list comprehensions, blocks indicated by
indentation and multiple return values. So your whole program ends up
being easy to express in Python, as compared to C#.

More information about the Python-list mailing list