Python vs. C#

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Aug 11 04:58:28 EDT 2003


"Brandon J. Van Every" <vanevery at 3DProgrammer.com> wrote in
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#?
> 
C# (or rather the CLR) has collection types that are superficially similar 
to Python's list and dict types. A lot of C# programming becomes much 
easier when you start using ArrayList et. al to do things in a Pythonic 
manner, BUT with C# you cannot manipulate objects without casting them back 
to a known type. This is a pain. If I have two objects and they are both 
integers, then in C# I have to cast them to 'int' before adding them. Or if 
they are strings I have to cast them to string before adding them. In 
Python I know they are a suitable type, so I just add them without thinking 
about it.

What this means is that you waste a lot of energy in the C# universe 
defining type-safe subclasses of the collections just to avoid cluttering 
your code with a lot of superfluous casts.

Python has a much more flexible syntax for function arguments. You can have 
optional arguments, keyword arguments, you can even have functions where 
you don't know the names of all the keyword arguments in advance. C# just 
has overloading, so you need to define a new overload for every optional 
argument and you can't call a function missing out some arguments from the 
middle of the list.

Tuple unpacking. C# just has naff reference arguments. Need I say more?

Slicing of sequences.

Python lets you write lists and dictionaries inline where you need them, 
much more convenient than in C#.

Functions are first class objects in Python. C# delegates have some neat 
features (async calls and multicasts), but the effort of using a delegate 
in most situations is just too much.

Don't get me wrong, C# is quite good, but it just has too much syntactic 
clutter obscuring the code.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list